UIImageView or Sprite withImage

Forums Programming cocos2d support (graphics engine) UIImageView or Sprite withImage

This topic contains 10 replies, has 6 voices, and was last updated by  Codemattic 4 years, 9 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
Author Posts
Author Posts
June 10, 2009 at 9:16 pm #215493


Hi, I posted this on the google discussion group, but it looks like the community is over here now on this new forum, so I’ve also moved my question over here. (sorry dont mean to double post)

Is it possible to add sprites/cocosnodes above (z order wise) a

UIImageView? Basically trying to get a photo from photo library, add

it to layer and then put some sprites on top. But the image view is

always on top.

Note, I also tried just loading the photo into a [Sprite

spriteWithCGImage:image.CGimage], and this works well for the z-

ordering, but it seems like the photo quality drops very noticeably

with this approach – as if the color pallete was reduced, or low-

quality-jpeg-like artifacts appear. It just appears that way to me, I

have no idea what it is actually doing to reduce the quality. So if I

can change a texture parameter or something to get better quality, I

am equally open to that. Although, I’ve tried experimenting with

GL_NEAREST, GL_LINEAR etc with not much luck.

Here is how i tried using the UIImageView

@implementation GameNode // superclass is Layer

-(id) init {

CGRect frame = [[[Director sharedDirector] openGLView] frame];

imageView = [[UIImageView alloc] initWithFrame:frame];

[[[Director sharedDirector] openGLView] addSubview:imageView];

[[[Director sharedDirector] openGLView]


return self;


-(void)imagePickerController:(UIImagePickerController *)picker

didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)

editingInfo {

[picker dismissModalViewControllerAnimated:YES];

imagePickerController.view.hidden = YES;

[imagePickerController.view removeFromSuperview];

imageView.hidden = NO;

imageView.image = image;

// make semi transparent, so i can see sprites behind the photo

imageView.alpha = .4;





June 11, 2009 at 2:53 am #248837

Steffen Altwiese

It sounds like the problem may be as simple as you are using 16 bit textures by default which is why your photo quality is degraded. It really helps if you tell us what version of cocos2d you are using. Riq wrote a great article about texture pixel formats here:


June 11, 2009 at 4:38 am #248838


I’m using the SVN trunk from about a week ago, which I guess is 0.8.?. And thanks for the tip, you were right again – default texture was RGBA4444, and changing it to 8888 worked great. Was not even aware of the different texture settings. Thank you sir.

June 11, 2009 at 9:38 am #248839


Probably the default pixel format in v0.8 should be the same as in v0.7: RGBA8888.

It’s causing many issues.

July 8, 2009 at 8:24 pm #248840


I have exactly the same problem with lifebythedrop! The UIImageView is always on top, no matter it is in a layer or not. The code

[[[Director sharedDirector] openGLView] sendSubviewToBack:imageView];

doesn’t work either.

Unluckily, I can’t use a Sprite instead as it is actually the iPhone camera view at the back. Would anyone has any suggestions? Thanks a lot.

July 12, 2009 at 5:52 pm #248841


I figure out

[[[Director sharedDirector] openGLView] sendSubviewToBack:imageView]

doesn’t work because the imageView is added to the openGLView. Seems cocos2d could not draw on the UI object. The only way to do so is using standard UI method, but it is not convenience as cocos2d when animation is involved.

July 21, 2009 at 1:37 am #248842



I was curious if you managed to figure out more on this subject? I am trying to do something similar to what you described and I was wondering if you could post more of your code. I have a Scene and a Layer and I am trying to open the image library or take a photo from the camera then put the selected image or new photo into a sprite. My problem is that I cannot get the UIImagePickerController to because visible. I debugged the code and it just skips right over the presentModalViewController message. I have the following code in my controller class

@implementation AppController

@synthesize window;

@synthesize layer;

// Initalize the main UIWindow

window = UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds;

// Enable Multiple Touches? No

[window setMultipleTouchEnabled:NO];

// Attach cocos2d to the window

[[Director sharedDirector] attachInWindow:window];

[[[Director sharedDirector] openGLView] addSubview:self];

// set landscape

[[Director sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft];

// Make the window visible

[window makeKeyAndVisible];

// Create scene

Scene* scene = [Scene node];

// Create and initialize custom layer

layer = [CameraImageLoadTest node];

// add out MainEvents Layer as a child of the main screen

[scene addChild:layer];

// Run

[[Director sharedDirector] runWithScene:scene];

UIImagePickerController* picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

picker.allowsImageEditing = YES;

// PROBLEM: Skips right over this message???

[self presentModalViewController:picker animated:YES];

[picker release];

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image

editingInfo:(NSDictionary *)editingInfo {

// Create Sprite from UIImage

Sprite* sprite = [Sprite spriteWithCGImage:image.CGImage];

CGSize size = [[Director sharedDirector] winSize];

sprite.position = ccp(size.width/2, size.width/2);

[layer addChild:sprite z:0 tag:kTagSprite];

[picker dismissModalViewControllerAnimated:YES];


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissModalViewControllerAnimated:YES];


Any help would be greatly appreciated as it seems you are far past this step.



July 21, 2009 at 2:30 am #248843


the -presentModalViewController:animated: message needs to be sent to a UIViewController, you are sending it to the your AppController.

July 21, 2009 at 2:35 am #248844


also this doesnt look right to me:

[[[Director sharedDirector] openGLView] addSubview:self];

-addSubview: is supposed to take a UIView, and you are sending your AppController.

July 21, 2009 at 4:14 am #248845



Thank you very much for the help (I was at my wits end and about to crack). I program all day in Java for the last 6 years and Objective-C plus learning the frameworks are killing me. Thank goodness CoCos is much easier to understand than most.

As for the problem at hand. I actually had my AppController class as an instance of UIViewController sorry for not stating that. I also realis this is probably very poor programming practice but I am just throwing things together for a pre-prototype proof of concept and just started working in Objective-C a few hours before this positing. Your second comment sent me on the right track to figure out the problem. First I wanted to say I don’t know where that line of code came from in your second post. It is clearly in my post but not anywhere in my actual code. I am really at a loss for where it came from. What it did get me to realize is the fact that I forgot to create a view for my UIViewController class. The only strange thing is I don’t know how to hide this view just like lifebythedrop’s trying to do with UIImageView. I finally just broke down and added the “attachInWindow” message after the picker was hidden. It seems there are several messages to move the view to the back but they seem to have no effect.

Again thank you very much!

July 21, 2009 at 5:24 am #248846


yeah, sometimes noticing something strange seemingly not related can point you in the right direction, glad it helped.

“I actually had my AppController class as an instance of UIViewController sorry for not stating that. I also realis this is probably very poor programming practice”

Well, it could work that way. But it seems better organized to have a separate View and App Controllers. Good luck with your project.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.