UIImagePickerController在iOS 11.1中工作,但在iOS 11.4中不工作

时间:2018-06-18 22:53:58

标签: ios camera uiimagepickercontroller photolibrary

我有一套相当典型的控件来拍照或从用户的照片库中选择。我在Xcode中的最新操作系统版本是11.1,图像选择器使用我的代码。 (我不知道是否可以在模拟器上运行更新的版本。)

当我在实际的iPhone上运行代码(使用iOS 11.4的5s)时,我从图像选择器中收到发现错误:

  

错误域= PlugInKit代码= 13“查询已取消”UserInfo = {NSLocalizedDescription =查询已取消}

尝试使用相机只会导致返回视图控制器,显然没有对新图像数据采取任何操作,也没有错误消息。

编辑:我对info.plist有相机和照片库权限,但它们似乎不会影响此问题。

这是相关代码(VC做了其他一些不相关的事情):

UserProfileViewController.h

#import <UIKit/UIKit.h>

@interface UserProfileViewController : UIViewController <NSURLSessionDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIScrollViewDelegate>
{
    __weak IBOutlet UIScrollView *scrolview;

}

@end

UserProfileViewController.m:

#import "UserProfileViewController.h"

. . . 

- (IBAction)takePhoto:(id)sender {

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIAlertController *errAlertController = [UIAlertController alertControllerWithTitle:@"Whoa!" message:@"This phone doesn't have a camera." preferredStyle:UIAlertControllerStyleAlert];
    [errAlertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:errAlertController animated:YES completion:nil];
}
else
{

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:NULL];

    }
}

- (IBAction)ChooseFromGallery:(id)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:picker animated:YES completion:NULL];

}

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


[picker dismissViewControllerAnimated:YES completion:^{
    UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];

    if ((chosenImage.size.height > 600.0) || (chosenImage.size.width > 800.0)){  // Need to scale down?
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(800.0f, 600.0f), NO, 0.0);
        [chosenImage drawInRect:CGRectMake(0, 0, 800, 600)];
        UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [self uploadPhoto:scaledImage];
    }
    else {
        [self uploadPhoto:chosenImage];
    }
    // "uploadPhoto" takes the JPG representation of the image and uploads it to a specific server path using HTTP POST. As mentioned, it worked in the simulator for iOS 11.1.
    }];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}

1 个答案:

答案 0 :(得分:0)

事实证明,图像实际上是在 中进行了更新,但直到应用程序被销毁并重新启动后才显示。

显然,这种效果是由于在dismissViewController的完成块中而不是在didFinishPickingMediaWithInfo的主块中完成图像处理引起的。将完成块设置为NULL并移动缩放和上载代码可解决该问题。