2011-07-20 43 views
0

我有一個CCLayer,我需要添加一個攝像頭,最重要的是,我需要一個自定義按鈕來關閉相機覆蓋到相機上。我最終需要在其上顯示CCSprites,但首先需要能夠關閉相機。從Cocos2D刪除UIImagePickerController

但是當我按一下按鈕,我得到SIGABRT或壞的EXE錯誤,這取決於我是否使用[[[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];  
} 

回答

1

我相信這個問題是這一行:

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];  
} 
+0

那正是它謝謝你! – daidai