2013-12-10 38 views
1

我有一個應用程序,需要拍攝一張照片,然後顯示給用戶。我打開相機,讓使用以下代碼的用戶拍照:如何在啓用ARC的情況下檢索使用UIImagePickerControllerDelegate拍攝的圖像?

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { 
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
    imagePicker.delegate = self; 
    imagePicker.sourceType = 
    UIImagePickerControllerSourceTypeCamera; 
    imagePicker.mediaTypes = @[(NSString *) kUTTypeImage]; 
    imagePicker.allowsEditing = NO; 
    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen; 

    [self presentViewController:imagePicker animated:YES completion:nil]; 
} else { 
    UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle: @"Camera failed to open" 
          message: @"Camera is not available" 
          delegate: nil 
          cancelButtonTitle:@"OK" 
          otherButtonTitles:nil]; 
    [alert show]; 
} 

這本相機視圖,以便用戶可以拍攝照片。我實現的方法來處理取消和完整的事件是這樣的:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    [self dismissViewControllerAnimated:YES completion:nil]; 

    UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage]; 
    [photoView setImage:image]; 
    [photoView setNeedsDisplay]; 
} 

的問題是,經過最後一個函數去後,形象是零。起初dismissViewControllerAnimated在函數的末尾,但在this SO post答案解釋說應該首先調用dismissViewControllerAnimated並且應該在從信息獲取圖像之前釋放選擇器。我試着把dismissViewControllerAnimated放在頂部,但是因爲我使用的是ARC,所以我不能釋放選擇器。

如何從信息中獲取圖像?

+0

該帖子建議你解僱後完成獲取圖像的塊。你嘗試過嗎? – giorashc

+0

在解散控制器之前使用您的代碼獲取圖像。 – Nirav

回答

1

你已經在你的代碼中設置picker.allowsEditing = NO;。那你爲什麼要檢索編輯後的圖像?它不會存在。不管怎麼說,如果你允許編輯,使用此:

photoView.image = info[UIImagePickerControllerEditedImage] ? info[UIImagePickerControllerEditedImage] : info[UIImagePickerControllerOriginalImage] 

如果用戶編輯照片,photoView.image將被設置爲編輯過的照片,否則將被設置爲原始照片。

如果您不允許編輯,只需使用UIImagePickerControllerOriginalImage鍵檢索圖像即可。

+1

不允許編輯imageImagePicker.allowsEditing = NO;因此只需要檢查原始圖像。 – selmad

+0

@selmad對不起,沒有得到你。你什麼意思? – duci9y

+0

我編輯了我的評論,不小心按下了回車。 – selmad

1

試試這個:

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
// or UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; 
-1

入住此方法,

(void) imagePickerController:(UIImagePickerController *)_picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
     NSLog(@"dictionary image info: %@",info); 
    } 

在這種方法中,打印你得到UIImagePickerControllerMediaTypeUIImagePickerControllerOriginalImage字典和檢查。 (在我的情況,我米獲得價值這樣UIImagePickerControllerMediaType = 「public.image」; UIImagePickerControllerOriginalImage =的UIImage:0x1dd79520"

現在,如果你正在爲獲得價值UIImagePickerControllerOriginalImage然後保存價值這的UIImage

現在用這個。

UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; 

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

希望這會對你有幫助。

相關問題