【问题标题】:iOS 11 UIImagePickerController after selection cancel button bugiOS 11 UIImagePickerController 后选择取消按钮错误
【发布时间】:2018-03-27 11:53:48
【问题描述】:

我正在使用 UIImagePickerController 来选择照片并裁剪图像。

这与在 https://forums.developer.apple.com/thread/88286

这是我的代码:

- (void) onTest:(UIButton *)btn {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    picker.delegate = self;
    picker.allowsEditing = YES;
    [picker setModalPresentationStyle: UIModalPresentationOverCurrentContext];
    [self presentViewController:picker animated:YES completion:nil];
}

这是视图层。我没有添加这个 UIView。 我无法按下取消按钮,也无法放大/缩小。

iOS 10 没有问题。我只有 iOS 11 有问题。

【问题讨论】:

  • 您是否尝试过bringSubViewToFront: 选择器将其带到此视图的前面以阻止触摸?
  • 你最后找到解决方案了吗?

标签: ios objective-c iphone uiimagepickercontroller ios11


【解决方案1】:

使用此代码打开照片库:

        UIImagePickerController *pickerView = [[UIImagePickerController alloc] init];
        pickerView.allowsEditing = YES;
        pickerView.delegate = self;
        [pickerView setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self presentViewController:pickerView animated:YES completion:nil];

并使用此委托将裁剪后的图像发送到您的视图控制器:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    UIImage *image = chosenImage;
   [picker dismissViewControllerAnimated:YES completion:NULL];

}

【讨论】:

    【解决方案2】:

    我有一个好主意来解决这个问题。

    将控制器(从中显示UIImagePickerController)设置为UIImagePickerController 的委托,然后符合UINavigationControllerDelegate,因为UIImagePickerControllerUINavigationController 的子类,然后实现方法:

    - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
      if ([UIDevice currentDevice].systemVersion.floatValue < 11) {
        return;
      }
      if ([viewController isKindOfClass:NSClassFromString(@"PUPhotoPickerHostViewController")]) {
        weakify(viewController);
        [viewController.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
          if (obj.frame.size.width < 42 && FLOAT_EQUAL(obj.frame.size.height, [[UIScreen mainScreen] bounds].size.height)) {
            strongify(viewController);
            [viewController.view sendSubviewToBack:obj];
            *stop = YES;
          }
        }];
      }
    }
    

    【讨论】:

    • 什么是弱化、强化和 FLOAT_EQUAL?我尝试了您的解决方案,但 xcode 显示错误函数“...”的隐式声明在 C99 中无效
    • weakController , FLOAT_EQUAL 是比较两个浮点值
    【解决方案3】:
    - (IBAction)addPhoto:(UIButton *)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:NULL];
    
    }
    

    【讨论】:

    • PUPhotoPickerHostViewController 是照片裁剪视图控制器。这个 viewController 有两个按钮。取消按钮和选择按钮。取消按钮不起作用.. PUPhotoPickerHostViewController 的取消按钮与委托无关。
    猜你喜欢
    • 2017-12-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2022-09-30
    • 2012-02-05
    • 2012-02-21
    • 1970-01-01
    相关资源
    最近更新 更多