0

I want to rotate image (left and right rotation) with angle 90.I am using following code it rotate left with angle 360.how can i rotate my image with angle 90.

-(IBAction)btnLeftRotateClicked:(id)sender
{


    CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);

    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

    rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
    rotationAnimation.duration = 0.25f;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1; 

    [imageAdjustView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

}

Thanks.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
M.S.B
  • 121
  • 1
  • 9

4 Answers4

1

Why don't you use the affineTransform if you just want to rotate the image in 2D

CGAffineTransformMakeRotation(-M_PI * 0.5);
ultragtx
  • 957
  • 8
  • 24
  • Because it won't work? - the keyPath is defined (by Apple) as a CATransform3D. c.f. (incredibly hard to find, because Apple doesn't link to it from anywhere in the CAAnimation class references where it's supposed to be!) http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/KVCAdditions.html#//apple_ref/doc/uid/TP40005299-SW6 – Adam Jun 04 '12 at 13:14
0

@M.S.B:

I think this link will help you.

How to Rotate a UIImage 90 degrees?

How to programmatically rotate image by 90 Degrees in iPhone?

You can refer to this answer by fbrereto in the above link

What about something like:

static inline double radians (double degrees) {return degrees * M_PI/180;}
UIImage* rotate(UIImage* src, UIImageOrientation orientation)
{
    UIGraphicsBeginImageContext(src.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orientation == UIImageOrientationRight) {
        CGContextRotateCTM (context, radians(90));
    } else if (orientation == UIImageOrientationLeft) {
        CGContextRotateCTM (context, radians(-90));
    } else if (orientation == UIImageOrientationDown) {
        // NOTHING
    } else if (orientation == UIImageOrientationUp) {
        CGContextRotateCTM (context, radians(90));
    }

    [src drawAtPoint:CGPointMake(0, 0)];

    return UIGraphicsGetImageFromCurrentImageContext();
}

EDIT: You can refer to answer given by Sabobin under the link How to programmatically rotate image by 90 Degrees in iPhone?. I have posted the same code so that you can refer it from here:

UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"my_image.png"]];

//set point of rotation
myImageView.center = CGPointMake(100.0, 100.0);

//rotate rect
myImageView.transform = CGAffineTransformMakeRotation(3.14159265/2);

Hope this helps you.

Community
  • 1
  • 1
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • @M.S.B: I have just edited my answer to make it more simpler for you. Check the **EDIT** section of my answer. You can apply that directly I think – Parth Bhatt Mar 16 '12 at 06:22
  • your edited code i use but its make only one time rotate i want every click it rotate.by the way if you have flip mirror image Horizontal code can you share with me? – M.S.B Mar 16 '12 at 08:03
  • @M.S.B: Can you post your code after the changes you have made? – Parth Bhatt Mar 16 '12 at 08:20
  • -(IBAction)btnRightRotateClicked:(id)sender { rotation=rotation-90; CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-rotation)); imageAdjustView.transform = transform; [UIView commitAnimations]; } i use now this for right rotation its work fine. – M.S.B Mar 16 '12 at 09:19
  • UIImage* rotate(UIImage* src, UIImageOrientation orientation) in ur function i not understand its class method or instance methos .can you right sample code how to call this function? – M.S.B Mar 16 '12 at 19:12
  • @M.S.B: Do not use `UIImage* rotate(UIImage* src, UIImageOrientation orientation) `. Please use the code in **EDIT** section of my answer. It would be very easy to implement it. So please use that. – Parth Bhatt Mar 18 '12 at 12:34
0

A rotation of 360 degree is no Rotation. Guess you mean 180 = PI. Thus, 90 degree = .5*PI.

Matthias
  • 8,018
  • 2
  • 27
  • 53
0

If you want to use some easy drop in classes that rotate a UIImage and also scale it, try looking at: http://bit.ly/w3r5t6 it is a UIImage category called WBImage

tams
  • 830
  • 5
  • 9