我有一個應用程序,需要拍攝一張照片,然後顯示給用戶。我打開相機,讓使用以下代碼的用戶拍照:如何在啓用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,所以我不能釋放選擇器。
如何從信息中獲取圖像?
該帖子建議你解僱後完成獲取圖像的塊。你嘗試過嗎? – giorashc
在解散控制器之前使用您的代碼獲取圖像。 – Nirav