GLImagePickerHelper is helper of UIImagePickerController.
If allowsEditing property of UIImagePickerController is YES, zoom scale of crop view is not correct with landscape image.
GLImagePickerHelper helps to correct zoom scale of crop view.
Furthermore GLImagePickerHelper can clip out the image in a circle.
There are two ways to use this in your project:
- Copy
Classes/ios/*.{h.m}
into your project - Install with CocoaPods to write Podfile
platform :ios
pod 'GLImagePickerHelper'
As first, call setup method.
- (void)setup;
Show hole cropping view or not show hole crop view. Default is holeCropping is YES.
@property (nonatomic) BOOL holeCropping;
For to correct zoom scale of crop view, call willShowViewController method in navigationController:willShowViewController:animated: method (UINavigationControllerDelegate).
- (void)willShowViewController:(UIViewController *)viewController;
For next time, need to call didFinishPickingMediaWithInfo: method in imagePickerController:didFinishPickingMediaWithInfo: method (UIImagePickerControllerDelegate).
- (NSDictionary *)didFinishPickingMediaWithInfo:(NSDictionary *)info;
#import "GLImagePickerHelper.h"
@interface ExampleViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic) GLImagePickerHelper *helper;
@end
@implementation ExampleViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.helper = [GLImagePickerHelper new];
[self.helper setup];
// self.helper.holeCropping = NO; // if you don't use hole cropping
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.helper viewWillAppear];
}
- (void)showImagePickerControllerSourceTypeCamera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
}
- (void)showImagePickerControllerSourceTypePhotoLibrary
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
info = [self.helper didFinishPickingMediaWithInfo:info];
UIImage *image = info[UIImagePickerControllerEditedImage];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
[picker dismissViewControllerAnimated:YES completion:nil];
});
}
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[self.helper willShowViewController:viewController];
}
@end
GLImagePickerHelper is available under the MIT license.