How to crop an image using YKImageCropper

Crop Image Swift

All the apps that work with images contain cropping feature. So, today we will cover that topic as well using the YKImageCropper library. Easy to use and compatible with both Swift and Objective-C.

First, install the library to your project via CocoaPods pod ‘YKImageCropper’, ‘~> 0.0.3’. 

 

If you don’t need to add it to some custom view then only call this snippet and you are good to go:

UIImage *image = [UIImage imageNamed:@"image.jpg"];
YKImageCropperViewController *vc = [[YKImageCropperViewController alloc] initWithImage:image];
vc.cancelHandler = ^() {
    // When cancel is tapped
};
vc.doneHandler = ^(UIImage *editedImage) {
    // When done is tapped
};

 

But, if you have some custom view inside your view controller where you need to implement a cropping feature, then you need to use this solution that I have written in Swift:

func callCropView(img: UIImage){
    cropView = YKImageCropperView(image: img,frame: CGRectZero)
    containerView.addSubview(cropView)
}

// then just call this line wherever you need to show the final cropped image
if let img = cropView.editedImage(){

}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *