Fastest approach for rotating an image on the iPhone
Question
I need to rotate a UIImage in response to user input. I'd like this to be as slick as possible and therefore would like to know which is the quickest way to perform the transformation and render. Ideally I'd like to stay within the UIKit or Quartz frameworks. The properties of the image are as follows:
- Size approximately 250x300 pixels
- Not opaque - needs to render with an alpha channel
What are the best approaches and practices for implementing this functionality?
Note: There is an approach described in this stackoverflow answer but I am uncertain if this is optimal. I will of course give this a try but I'd be keen to know if this is considered to be the 'best practice'.
Solution
This example of applying a Core Animation layer to a UIImage might provide some inspiration:
UIImage* rotatingImage = [UIImage imageNamed:@"someImage.png"];
CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);
CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
rotationAnimation.duration = 0.25f;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 10;
[rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
OTHER TIPS
If you are displaying your UIImage in a UIImageView, why not just set the transform property on the view ?
myimageView.transform = CGAffineTransformMakeRotation(<angle in radians>);
It's as simple as that, and for a small image like your talking about this is highly performant - all UIKit views are layer backed, so it's hardware accelerated. This property is also animatable.