RSKImageCropper 0,-1,2,1,2,0,5 travis-ci ruby CocoaPods

An image cropper / photo cropper for iOS like in the Contacts app with support for landscape orientation.

2 years after MIT

RSKImageCropper Build Status CocoaPods Coverage Status Carthage compatible

Sample

An image cropper for iOS like in the Contacts app with support for landscape orientation.

Installation

RSKImageCropper requires iOS 6.0 or later.

Using CocoaPods

  1. Add the pod RSKImageCropper to your Podfile.

    pod 'RSKImageCropper'
  2. Run pod install from Terminal, then open your app's .xcworkspace file to launch Xcode.

  3. Import the RSKImageCropper.h header. Typically, this should be written as #import <RSKImageCropper/RSKImageCropper.h>

Using Carthage

  1. Add the ruslanskorb/RSKImageCropper project to your Cartfile.

    github "ruslanskorb/RSKImageCropper"
  2. Run carthage update, then follow the additional steps required to add the iOS and/or Mac frameworks into your project.

  3. Import the RSKImageCropper framework/module.

    • Using Modules: @import RSKImageCropper
    • Without Modules: #import <RSKImageCropper/RSKImageCropper.h>

Basic Usage

Import the class header.

#import <RSKImageCropper/RSKImageCropper.h>

Just create a view controller for image cropping and set the delegate.

- (IBAction)onButtonTouch:(UIButton *)sender
{
    UIImage *image = [UIImage imageNamed:@"image"];
    RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:image];
    imageCropVC.delegate = self;
    [self.navigationController pushViewController:imageCropVC animated:YES];
}

Delegate

RSKImageCropViewControllerDelegate provides four delegate methods. To use them, implement the delegate in your view controller.

@interface ViewController () <RSKImageCropViewControllerDelegate>

Then implement the delegate functions.

// Crop image has been canceled.
- (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller
{
    [self.navigationController popViewControllerAnimated:YES];
}

// The original image has been cropped.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
                   didCropImage:(UIImage *)croppedImage
                  usingCropRect:(CGRect)cropRect
{
    self.imageView.image = croppedImage;
    [self.navigationController popViewControllerAnimated:YES];
}

// The original image has been cropped. Additionally provides a rotation angle used to produce image.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
                   didCropImage:(UIImage *)croppedImage
                  usingCropRect:(CGRect)cropRect
                  rotationAngle:(CGFloat)rotationAngle
{
    self.imageView.image = croppedImage;
    [self.navigationController popViewControllerAnimated:YES];
}

// The original image will be cropped.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
                  willCropImage:(UIImage *)originalImage
{
    // Use when `applyMaskToCroppedImage` set to YES.
    [SVProgressHUD show];
}

DataSource

RSKImageCropViewControllerDataSource provides three data source methods. The method imageCropViewControllerCustomMaskRect: asks the data source a custom rect for the mask. The method imageCropViewControllerCustomMaskPath: asks the data source a custom path for the mask. The method imageCropViewControllerCustomMovementRect: asks the data source a custom rect in which the image can be moved. To use them, implement the data source in your view controller.

@interface ViewController () <RSKImageCropViewControllerDataSource>

Then implement the data source functions.

// Returns a custom rect for the mask.
- (CGRect)imageCropViewControllerCustomMaskRect:(RSKImageCropViewController *)controller
{
    CGSize maskSize;
    if ([controller isPortraitInterfaceOrientation]) {
        maskSize = CGSizeMake(250, 250);
    } else {
        maskSize = CGSizeMake(220, 220);
    }

    CGFloat viewWidth = CGRectGetWidth(controller.view.frame);
    CGFloat viewHeight = CGRectGetHeight(controller.view.frame);

    CGRect maskRect = CGRectMake((viewWidth - maskSize.width) * 0.5f,
                                 (viewHeight - maskSize.height) * 0.5f,
                                 maskSize.width,
                                 maskSize.height);

    return maskRect;
}

// Returns a custom path for the mask.
- (UIBezierPath *)imageCropViewControllerCustomMaskPath:(RSKImageCropViewController *)controller
{
    CGRect rect = controller.maskRect;
    CGPoint point1 = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect));
    CGPoint point2 = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
    CGPoint point3 = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));

    UIBezierPath *triangle = [UIBezierPath bezierPath];
    [triangle moveToPoint:point1];
    [triangle addLineToPoint:point2];
    [triangle addLineToPoint:point3];
    [triangle closePath];

    return triangle;
}

// Returns a custom rect in which the image can be moved.
- (CGRect)imageCropViewControllerCustomMovementRect:(RSKImageCropViewController *)controller
{
    // If the image is not rotated, then the movement rect coincides with the mask rect.
    return controller.maskRect;
}

Coming Soon

  • If you would like to request a new feature, feel free to raise as an issue.

Demo

Build and run the RSKImageCropperExample project in Xcode to see RSKImageCropper in action. Have fun. Fork and send pull requests. Figure out hooks for customization.

Contact

Ruslan Skorb

License

This project is is available under the MIT license. See the LICENSE file for more info. Attribution by linking to the project page is appreciated.

Related Repositories

react-native-image-crop-picker

react-native-image-crop-picker

iOS/Android image picker with support for camera, video compression, multiple im ...

awesome-objective-c

awesome-objective-c

A curated list of awesome Objective-C frameworks, libraries and software. ...

awesome-ios

awesome-ios

A curated list of awesome iOS ecosystem, including Objective-C and Swift Project ...

awesome-ios

awesome-ios

A curated list of awesome iOS ecosystem, including Objective-C and Swift Project ...


Top Contributors

ruslanskorb dbergman jjochen pronebird ogoldfinger Serchinastico timshadel

Releases

-   1.5.1 zip tar
-   1.5.0 zip tar
-   1.4.1 zip tar
-   1.4.0 zip tar
-   1.3.2 zip tar
-   1.3.1 zip tar
-   1.3.0 zip tar
-   1.2.1 zip tar
-   1.2.0 zip tar
-   1.1.5 zip tar
-   1.1.3 zip tar
-   1.1.2 zip tar
-   1.1.1 zip tar
-   1.1.0 zip tar
-   1.0.0 zip tar
-   0.5.0 zip tar
-   0.4.1 zip tar
-   0.4.0 zip tar
-   0.3.1 zip tar
-   0.3.0 zip tar
-   0.2.0 zip tar
-   0.1.0 zip tar