0
votes

Using Gesture Recognizers such as Pan, Pinch, and Rotate, is it at all possible to manipulate a UIImage within a UIImageView?

Whenever I try to define the gesture recognizer for the UIImage, I get an error stating

No visible UIImage supports 'addGestureRecognizer:'

With this code:

UIRotationGestureRecognizer *rotateGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateImage:)];
[imageStrip addGestureRecognizer:rotateGesture];

Is there anyway to transform a UIImage inside of a UIImageView without transforming the UIImageView?

2
Actually, modifying the image data itself could be such a complicated thing... Why is that you don't want to transform the UIImageView? I ask it because you have the possibility to generate a new UIImage from any UIView (something like taking a snapshot), and may be it would be ok for you. - Ricard Pérez del Campo
I was aiming for a special edit effect in my application, which I ended up using a UIView with the transformable UIImageView inside of it. Then I set the clipsToBounds = YES - Sam Spencer

2 Answers

5
votes

You could add gesture recognizers to any kind of UIView and in your event handling methods you could redraw UIImage to a freshly created CGContextRef. This context would have to be transformed with CGContextRotateCTM, CGContextScaleCTM , CGContextTranslateCTM that would be called with parameters matching the ones you receive from gesture recognizers. That's the hard way.

Alternatively you could create a scratch UIImageView with your image, transform it with gesture recognizers then render it to a CGContextRef and get the transformed image out of this context.

Certainly doable, but not trivial.

2
votes

UIGestureRecognizer can only be attached to a UIView - UIImage is not a descendant of UIView.

This is because an UIView (or it's descendant) can receive touches - UIImage can not.

Please see Gestures Recognizers Are Attached to a View.

You could recalculate/rerender the UIImage based on parameters after user is done manipulating it. On how to do this: it really depends on what you want to achieve.