5

我正在使用 UIImagePickerController 来允许用户上传照片以在我的应用程序中使用。选择要上传的照片后,系统会提示用户裁剪他/她的照片(因为我已设置imagePickerController.allowsEditing = YES)。但是,如果照片是横向的(即,宽大于高),用户目前可以以不输出方形图像的方式裁剪照片。这是因为裁剪横向照片时的默认缩放级别是 aspectFit 而不是 aspectFill(因为它适用于纵向照片)。

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

4

1 回答 1

1

无法以UIImagePickerController受支持的方式调整裁剪/编辑功能的行为。您可能会深入研究UIImagePickerController控制器/视图层次结构并尝试弄清楚它是如何工作的,但这不是一件易于维护或令人愉快的事情。

话虽如此,UIImagePickerController是 的子类UINavigationController,所以没有什么能阻止您实现自己的图像编辑视图控制器并将其推送到UIImagePickerController. 这可能不会太难,你可以把挑选的UIImage东西扔进一个UIScrollView带有显示裁剪区域的矩形覆盖层中,做一些数学运算,然后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
于 2013-08-10T17:59:16.253 回答