Sam Spencer
Sam Spencer

Reputation: 8609

Manipulating UIImage with GestureRecognizers - NOT UIImageView

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?

Upvotes: 0

Views: 203

Answers (2)

Bartosz Ciechanowski
Bartosz Ciechanowski

Reputation: 10333

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.

Upvotes: 5

Rok Jarc
Rok Jarc

Reputation: 18875

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.

Upvotes: 2

Related Questions