R – Fastest approach for rotating an image on the iPhone


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 250×300 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'.

Best 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"];