Question

I want to add a Pinch and Pan gesture that will crop the UIImage views on my ViewController. To crop the images, just pinch/pan the appropriate image and it will resize.

I currently have the Take Pic and Choose Image buttons grab the appropriate images into the UIImageView boxes accordingly, but I want to be able to pinch and pan to size and crop the image in the view.

I want the box to stay fixed at its current size, and the image within will be the one resizing and cropping.

PS. I am using storyboard

Here is a screenshot of what I have. http://i.stack.imgur.com/chkk5.png

I was thinking that the right direction would be to add a swipe and pan gesture to each uiimageviews in storyboard, but not sure what to do after.

Was it helpful?

Solution

My general strategy for this in the past has to been place the UIImageView inside another UIView (in order to clip it) and add a UIPinchGestureRecognizer and a UIPanGestureRecognizer to the parent UIView. When you receive events for either of these you apply the appropriate transformation to the transform property of the UIImageView.

When scaling, for example, you would do the following:

-(void)scale:(UIPinchGestureRecognizer*)pinch
{
 float scale = pinch.scale;
 imageView.transform = CGAffineTransformScale(imageView.transform, scale, scale);
 pinch.scale = 1;
}

The final step of reseting the scale is important because you are simply scaling the existing transform each time rather than a base identity transform. You should find that you will be able to handle translating the transform by using a UIPanGestureRecognizer and its translationInView: method (remember you will also have to reset this with setTranslationInView:CGPointZero.

Finally, in order to get a cropped/clipped UIImage out you can either capture the view's contents (I believe the transform property is only respected on iOS 7+) or alternatively render the UIImage into a CGContextRef, and using the same transform to transform the CGContextRef.

OTHER TIPS

You can use below link...

Crop image

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top