开发者

Remove UIImagePickerController from Cocos2D

I have a CCLayer which I need to add a camera to, on top of that I need a custom button to close the camera overlayed onto of the camera. I eventually need to display CCSprites on top of it all, but first need to be able to dismiss the camera.

But when I click on the button, I get SIGABRT or BAD EXE errors depending on whether I use [[[CCDirector sharedDirector] openGLView] sendSubviewToBack:uip.view]; or [uip.view removeFromSuperview];

-(void) displayCamera
{

uip = [[[UIIma开发者_JAVA技巧gePickerController alloc] init] autorelease];
uip.sourceType = UIImagePickerControllerSourceTypeCamera;
uip.showsCameraControls = NO;
uip.toolbarHidden = YES;
uip.navigationBarHidden = YES;
uip.wantsFullScreenLayout = YES;

[[[CCDirector sharedDirector] openGLView] addSubview:uip.view];

UIButton *arrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[arrowButton addTarget:self 
           action:@selector(arrowButtonClicked:)
 forControlEvents:UIControlEventTouchUpInside];

UIImage *imgNormal = [UIImage imageNamed:@"btn_next_norm.png"];
[arrowButton setImage:imgNormal forState:UIControlStateNormal];

UIImage *imgPressed = [UIImage imageNamed:@"btn_next_pressed.png"];
[arrowButton setImage:imgPressed forState:UIControlStateHighlighted];

arrowButton.frame = CGRectMake(screenSize.width - 48.0, screenSize.height - 37.0, 48.0, 37.0);

[[[CCDirector sharedDirector] openGLView] addSubview:arrowButton];
}

-(void)arrowButtonClicked:(id)sender
{
 // close / hide camera 

[[[CCDirector sharedDirector] openGLView] sendSubviewToBack:uip.view];

// or maybe [uip.view removeFromSuperview];

// and then go to another scene

LoadingScene* scene = [LoadingScene sceneWithTargetScene:TargetSceneEndExperienceScene];
[[CCDirector sharedDirector] replaceScene:scene];    
}


I believe the issue is this line:

uip = [[[UIImagePickerController alloc] init] autorelease];

Using autorelease in this case doesn't work because the UIImagePickerController is not retained by it's view, so you need to make sure that you retain it yourself. Instead of autoreleasing it, I would hold onto the reference to it, and then after you remove it from the view, I would release it then. I've changed your code to show what I mean:

-(void) displayCamera
{

uip = [[UIImagePickerController alloc] init];  // Don't autorelease it here
uip.sourceType = UIImagePickerControllerSourceTypeCamera;
uip.showsCameraControls = NO;
uip.toolbarHidden = YES;
uip.navigationBarHidden = YES;
uip.wantsFullScreenLayout = YES;

[[[CCDirector sharedDirector] openGLView] addSubview:uip.view];

UIButton *arrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[arrowButton addTarget:self 
           action:@selector(arrowButtonClicked:)
 forControlEvents:UIControlEventTouchUpInside];

UIImage *imgNormal = [UIImage imageNamed:@"btn_next_norm.png"];
[arrowButton setImage:imgNormal forState:UIControlStateNormal];

UIImage *imgPressed = [UIImage imageNamed:@"btn_next_pressed.png"];
[arrowButton setImage:imgPressed forState:UIControlStateHighlighted];

arrowButton.frame = CGRectMake(screenSize.width - 48.0, screenSize.height - 37.0, 48.0, 37.0);

[[[CCDirector sharedDirector] openGLView] addSubview:arrowButton];
}

-(void)arrowButtonClicked:(id)sender
{
 // close / hide camera 

[uip.view removeFromSuperview];
[uip release];  // Release here
uip = nil;

// and then go to another scene

LoadingScene* scene = [LoadingScene sceneWithTargetScene:TargetSceneEndExperienceScene];
[[CCDirector sharedDirector] replaceScene:scene];    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜