0

我想在用戶在我的應用程序的相機選項卡上標籤以拍攝照片(類似於Instagram等許多照片拍攝應用程序)時立即呈現UIImagePickerController。目前,我有一個名爲CameraViewController的VC,它顯示從UIImagePickerController拍攝的照片,並在CameraViewController的功能我提供ImagePickerController模式當我檢測到沒有圖片已被採取。當在View Controller生命週期中呈現UIImagePickerController時?

雖然這工作的大部分時間,我注意到,有時我會得到一個異常說:

「終止應用程序由於未捕獲的異常‘NSInvalidArgumentException’,理由是:「應用程序試圖把模態的主動控制器「。

根據這裏的一些SO問題的建議,我也嘗試在viewDidAppear中介紹控制器。但是,使用viewDidAppear時,模式ImagePicker永遠不會被解僱,並且我一直收到錯誤「不平衡呼叫開始/結束外觀轉換」。

我的問題是:

  1. 我目前呈現ImagePickerController模態,爲用戶拍攝照片,然後解僱一次拍攝照片。通常情況下,ImagePickerControllers是最可靠/無問題的方式。
  2. 還有哪些地方可以考慮將ImagePickerController放入視圖生命週期以獲得所需的效果?

這裏是我的代碼:

viewWillAppear

BOOL modalPresent = (BOOL)(self.presentedViewController); 
if (!appDelegate.imageStorageDictionary[@"picture1"]){ 
    if (modalPresent == NO){ 
     [self presentViewController:self.imagePickerController animated:NO completion:nil]; 
    } 
} 

imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

//Other lines omitted 
if (![[self imagePickerController] isBeingDismissed]) 
    [self dismissViewControllerAnimated:NO completion:nil]; 

回答

1

我沒有得到太多關於您對-viewWillAppear的問題,但有一件事你必須知道該-viewWillAppear將被稱爲多次(s)。原因是,第一個電話將是當你的viewController加載和viewDidLoad,並會顯示將照常調用,其次,當你已經提出modalViewController (imagePickerController),當它被再次解僱viewWillAppear將被調用,因爲viewController的視圖即將出現..

其次關於你想現在相機的功能,當用戶點擊拍照按鈕,這樣做:

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
{ 
    if(myPicker) 
    { 
     [myPicker release]; 
     myPicker = nil; 
     } 
myPicker = [[UIImagePickerController alloc] init]; 

    [self presentViewController:myPicker animated:YES completion:NULL]; 
} 
else 
{ 
    UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not Available" message:@"Camera Not Available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    altnot.tag=103; 
    [altnot show]; 
    [altnot release]; 
} 

採摘已拍攝圖像..

- (空)ChooseAlreadyCapturedPhoto {

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) 
{ 
    if(myPicker) 
    { 
     [myPicker release]; 
     myPicker = nil; 
    } 
    myPicker   = [[UIImagePickerController alloc] init]; 
    myPicker.delegate = self; 
    myPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 

    myPicker.allowsEditing = NO; 

} 
[self presentViewController:myPicker animated:YES completion:NULL]; 

}

注:我每次發佈選擇器的之前分配的對象時,我使用它,但它是沒有必要的。你可以使用我的選擇器的唯一單一實例,不要標記錯誤。並使用imagePicker-controller裏面的viewController是絕對好的,它不會傷害..如果你提出和解僱modalViewController一個沒有。時間,然後viewWill出現也會被調用,每次你解僱它。