gpt4 book ai didi

ios - 如何在启用 ARC 的情况下检索使用 UIImagePickerControllerDelegate 拍摄的图像?

转载 作者:行者123 更新时间:2023-11-29 12:58:09 27 4
gpt4 key购买 nike

我有一个应用程序需要拍照然后显示给用户。我使用以下代码打开相机让用户拍照:

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

这会呈现相机 View ,以便用户可以拍照。我实现了这样的方法来处理取消和完成事件:

- (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,所以我无法释放选择器。

如何从信息中获取图片?

最佳答案

您已在代码中设置了 picker.allowsEditing = NO;。那你为什么要检索编辑过的图像?它不会存在。不管怎样,如果你允许编辑,使用这个:

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

如果用户编辑过照片,photoView.image 将设置为编辑后的照片,否则将设置为原始照片。

如果您不允许编辑,只需使用 UIImagePickerControllerOriginalImage 键检索图像即可。

关于ios - 如何在启用 ARC 的情况下检索使用 UIImagePickerControllerDelegate 拍摄的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20490652/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com