设置最小缩放级别

设置最小缩放级别

本文介绍了为UIImagePickerController裁剪设置最小缩放级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

UIImagePickerController 裁剪/编辑功能的行为。你可以潜入到 UIImagePickerController 控制器/视图层次结构,并尝试找出它是如何工作的,但这不是一个非常可维护或令人愉快的事情。



话虽如此, UIImagePickerController UINavigationController 的子类没有什么阻止你实现自己的图像编辑视图控制器,并推送到 UIImagePickerController 。这可能不会太难,你可以把选择的 UIImage 放入一个 UIScrollView 作物区域,做一些数学,并自己裁剪 UIImage 。在这种情况下,你显然可以完全控制的功能,我敢打赌,花费更少的时间实现比spelunking到 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 completed:^ {
_imagePickerController = nil;
}];
}


#pragma mark ImageEditorViewControllerDelegate
- (void)imageEditorViewController:(ImageEditorViewController *)imageEditorViewController didFinishWithInfo:(NSDictionary *)info {
// TODO:处理编辑的媒体

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

然后你的编辑视图会有一个接口这个(具体针对你的需要):

  @protocol ImageEditorViewControllerDelegate; 

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

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

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


I am using a UIImagePickerController to allow the user to upload a photo for use in my application. After selecting a photo to upload, the user is then prompted to crop his/her photo (since I have set imagePickerController.allowsEditing = YES). However, if the photo is landscape (i.e., wider than it is taller), it is currently possible for the user to crop the photo in a way that does not output a square image. This is because the default zoom level when cropping landscape photos is aspectFit rather than aspectFill (as it is for portrait photos).

Is it possible to set the minimum zoom level for UIImagePickerController's edit mode? I suppose I could simply automatically crop the image if the output is not square, but I would rather that the edit mode of UIImagePickerController convey this to the user instead of doing it for him/her.

解决方案

It is not possible to adjust the behavior of the UIImagePickerController crop/edit feature in a supported manner. You could potentially dig into the UIImagePickerController controller/view hierarchy and try to figure out how it works, but that is not a very maintainable or pleasant thing to do.

Having said that, the UIImagePickerController is a subclass of UINavigationController, so there's nothing stopping you from implementing your own image editing view controller and pushing it onto the UIImagePickerController. This probably wouldn't be too hard, you could just throw the picked UIImage into a UIScrollView with a rectangular overlay showing the crop area, do some math, and crop the UIImage yourself. You would obviously have full control over the functionality in this case and I bet it would take less time to implement than spelunking into the guts of UIImagePickerController.

I would probably set it up whatever view controller presented the picker like this:

@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

And then your editing view would have an interface like this (with an implementation specific to your needs):

@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

这篇关于为UIImagePickerController裁剪设置最小缩放级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:37