views:

656

answers:

2

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'.

+3  A: 

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"];
Nathan de Vries
Thanks for sharing this Core Animation variant. I'll give this a try also.
teabot
I agree with the Core Animation suggestion. If you do animated rotation via Quartz, you'll see terrible performance because of all the redraws you'll need. You may also wish to check out http://stackoverflow.com/questions/486609/how-can-i-use-animation-in-cocos2d and http://stackoverflow.com/questions/839296/how-can-i-rotate-a-uiimageview-with-respect-to-any-point-other-than-its-center
Brad Larson
A: 

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.

Rennarda