设定最低缩放级别种植的UIImagePickerController(Set minimum zoo

2019-07-05 01:36发布

我使用的是的UIImagePickerController允许用户上传在我的应用程序中使用的照片。 选择要上传的照片后,用户则提示裁剪他/她的照片(因为我已经设置imagePickerController.allowsEditing = YES )。 然而,如果照片是横向(即宽度大于高),它是目前可能的是,用户裁剪在不输出一个方图像的方式的照片。 这是因为种植风景照片时的默认缩放级别aspectFit而非aspectFill(因为它是人像照片)。

是否有可能设置的UIImagePickerController的编辑模式的最低缩放级别? 我想,如果输出不是正方形的,我可以简单地自动裁剪图像,但我宁愿说的UIImagePickerController的编辑方式传达给用户,而不是做他/她的。

Answer 1:

这是不可能的调整行为UIImagePickerController在支持的方式作物/编辑功能。 你可能挖掘到UIImagePickerController控制器/视图层次,并试图弄清楚它是如何工作的,但不是做一个非常维护或惬意的事情。

说了这么多,该UIImagePickerController是的子类UINavigationController ,所以没有什么距离实现自己的图像编辑视图控制器,将其推到阻止你UIImagePickerController 。 这可能不会太硬,你可以随便扔拾取UIImageUIScrollView与显示裁剪区域的矩形叠加,做一些数学和裁剪UIImage自己。 你显然有超过在这种情况下,功能全面控制和我打赌它会采取低于洞穴探险到的胆量执行时间UIImagePickerController

我可能会设置它的任何视图控制器提出了这样的选择器:

@interface MainViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, ImageEditorViewControllerDelegate>
@end

@implementation MainViewController {
    UIImagePickerController* _imagePickerController;
}
#pragma mark IBAction
- (IBAction)pickImage:(id)sender {
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    _imagePickerController.allowsEditing = NO;

    [self presentViewController:_imagePickerController animated:YES completion:nil];
}


#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    ImageEditorViewController* imageEditorViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ImageEditor"];
    imageEditorViewController.delegate = self;
    imageEditorViewController.imageToEdit = info[UIImagePickerControllerOriginalImage];

    [_imagePickerController pushViewController:imageEditorViewController animated:YES];
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:^{
        _imagePickerController = nil;
    }];
}


#pragma mark ImageEditorViewControllerDelegate
- (void)imageEditorViewController:(ImageEditorViewController *)imageEditorViewController didFinishWithInfo:(NSDictionary *)info {
    // TODO: Handle the edited media

    [self dismissViewControllerAnimated:YES completion:^{
        _imagePickerController = nil;
    }];
}
@end

然后你的编辑视图就会有这样的接口(一个实现您的具体需要):

@protocol ImageEditorViewControllerDelegate;

@interface ImageEditorViewController : UIViewController
@property(nonatomic, strong) UIImage* imageToEdit;

@property(nonatomic, weak) id <ImageEditorViewControllerDelegate> delegate;
@end

@protocol ImageEditorViewControllerDelegate
- (void)imageEditorViewController:(ImageEditorViewController*)imageEditorViewController didFinishWithInfo:(NSDictionary*)info;
@end


文章来源: Set minimum zoom level for UIImagePickerController cropping