【问题标题】:UIImagePickerController works in iOS 11.1 but not in iOS 11.4UIImagePickerController 适用于 iOS 11.1 但不适用于 iOS 11.4
【发布时间】:2018-11-27 19:57:26
【问题描述】:

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

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

Error Domain=PlugInKit Code=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];

}

【问题讨论】:

    标签: ios camera uiimagepickercontroller photolibrary


    【解决方案1】:

    事实证明,图片实际上正在在 uploadPhoto 函数中更新,但直到应用程序被销毁并重新启动后才显示。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多