groovelab / GLImagePickerHelper

GLImagePickerHelper is helper of UIImagePickerController

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GLImagePickerHelper

GLImagePickerHelper is helper of UIImagePickerController.

If allowsEditing property of UIImagePickerController is YES, zoom scale of crop view is not correct with landscape image.

not correct zoom scale

GLImagePickerHelper helps to correct zoom scale of crop view.

correct zoom scale

Furthermore GLImagePickerHelper can clip out the image in a circle.

correct zoom scale

Installation

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'

Usage

Setup

As first, call setup method.

- (void)setup;

Show hole cropping view

Show hole cropping view or not show hole crop view. Default is holeCropping is YES.

@property (nonatomic) BOOL holeCropping;

Adjust zoom scale of crop view

For to correct zoom scale of crop view, call willShowViewController method in navigationController:willShowViewController:animated: method (UINavigationControllerDelegate).

- (void)willShowViewController:(UIViewController *)viewController;

After picking image

For next time, need to call didFinishPickingMediaWithInfo: method in imagePickerController:didFinishPickingMediaWithInfo: method (UIImagePickerControllerDelegate).

- (NSDictionary *)didFinishPickingMediaWithInfo:(NSDictionary *)info;

Example

UIViewController

#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

License

GLImagePickerHelper is available under the MIT license.

About

GLImagePickerHelper is helper of UIImagePickerController

License:MIT License


Languages

Language:Objective-C 96.5%Language:Ruby 3.5%