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 = [[[UIImagePickerController 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:
Using
autoreleasein this case doesn’t work because theUIImagePickerControlleris 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: