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
.