我正在使用UIImagePickerController来允许用户上传照片以便在我的应用程序中使用。选择要上传的照片后,系统会提示用户裁剪他/她的照片(因为我已设置imagePickerController.allowsEditing = YES
)。然而,如果照片是横向的(即,比它更高则更宽),则用户当前可以以不输出正方形图像的方式裁剪照片。这是因为裁剪风景照片时的默认缩放级别是aspectFit而不是aspectFill(因为它适用于肖像照片)。
是否可以为UIImagePickerController的编辑模式设置最小缩放级别?我想如果输出不是正方形,我可以简单地自动裁剪图像,但我宁愿UIImagePickerController的编辑模式将此传达给用户而不是为他/她做。
答案 0 :(得分: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