Objective c 从Cocos2D中卸下UIImagePickerController
我有一个CCLayer,我需要添加一个摄像头,在上面我需要一个自定义按钮来关闭叠加在摄像头上的摄像头。我最终需要在上面显示ccsprite,但首先需要能够关闭相机 但是当我点击按钮时,我会得到SIGABRT或BAD EXE错误,这取决于我是否使用Objective c 从Cocos2D中卸下UIImagePickerController,objective-c,cocos2d-iphone,uiimagepickercontroller,Objective C,Cocos2d Iphone,Uiimagepickercontroller,我有一个CCLayer,我需要添加一个摄像头,在上面我需要一个自定义按钮来关闭叠加在摄像头上的摄像头。我最终需要在上面显示ccsprite,但首先需要能够关闭相机 但是当我点击按钮时,我会得到SIGABRT或BAD EXE错误,这取决于我是否使用[[CCDirector sharedDirector]openGLView]sendSubviewToBack:uip.view]或[uip.view removeFromSuperview] -(void) displayCamera { uip
[[CCDirector sharedDirector]openGLView]sendSubviewToBack:uip.view]代码>或[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];
}
我认为问题在于这一行:
uip = [[[UIImagePickerController alloc] init] autorelease];
在这种情况下使用autorelease
不起作用,因为UIImagePickerController
的视图没有保留它,所以您需要确保自己保留它。我不会自动删除它,而是保留对它的引用,然后在您从视图中删除它之后,我会释放它。我已更改了您的代码以显示我的意思:
-(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];
}