Rotate image in UIImageView

16,836

Solution 1

You can rotate the image with the following code. Note, this uses a CGImageRef which you can get from a UIImage via

CGImageRef imageRef = [self CGImageRotatedByAngle:[image CGImage] angle:30];

Once you get the rotated image, you can set the ImageView's image to your new rotated image like this:

UIImage* img = [UIImage imageWithCGImage: imageRef];
myImageView.image = img;

here is a method that will rotate the imageRef:

- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle
{

    CGFloat angleInRadians = angle * (M_PI / 180);
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGRect imgRect = CGRectMake(0, 0, width, height);
    CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef bmContext = CGBitmapContextCreate(NULL,
                                                   rotatedRect.size.width,
                                                   rotatedRect.size.height,
                                                   8,
                                                   0,
                                                   colorSpace,
                                                   kCGImageAlphaPremultipliedFirst);
    CGContextSetAllowsAntialiasing(bmContext, YES);
    CGContextSetShouldAntialias(bmContext, YES);
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
    CGColorSpaceRelease(colorSpace);
    CGContextTranslateCTM(bmContext,
                          +(rotatedRect.size.width/2),
                          +(rotatedRect.size.height/2));
    CGContextRotateCTM(bmContext, angleInRadians);
    CGContextTranslateCTM(bmContext,
                          -(rotatedRect.size.width/2),
                          -(rotatedRect.size.height/2));
    CGContextDrawImage(bmContext, CGRectMake(0, 0,
                                             rotatedRect.size.width,
                                             rotatedRect.size.height),
                       imgRef);



    CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
    CFRelease(bmContext);
    [(id)rotatedImage autorelease];

    return rotatedImage;
}

Solution 2

Take a look at rotating the ImageView's layer. You will need to import the QuartzCore library to make that accessible.

myImageView.layer.transform = CGAffineTransformMakeRotation (1.5);

Solution 3

Please try this, it works for me:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatCount:10];
myImgView.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];

Good luck!

Solution 4

The following code works fine with Swift:

let img = UIImage(CGImage: self.imageView.image!.CGImage, scale: 1.0, orientation: UIImageOrientation.Right)
self.imageView.image = img
self.imageView.setNeedsLayout()
Share:
16,836

Related videos on Youtube

gcalikpl
Author by

gcalikpl

Updated on June 18, 2022

Comments

  • gcalikpl
    gcalikpl almost 2 years

    Is it possible to rotate only image in UIImageView? I'm looking for information about it, but i only found information how rotate UIImageVeiw.

  • gcalikpl
    gcalikpl over 12 years
    Thank you. It's exactly what I want.
  • heximal
    heximal over 10 years
    Someone may face unpredictable result as I recently did - the final image was losing its original size and cropped corners. So I've slighlty modified the function to get correct result. If someone's wondering my solution is here: stackoverflow.com/questions/19219855/…
  • fabian789
    fabian789 over 9 years
    had to use setAffineTransform
  • matangs
    matangs over 8 years
    After rotation image size will change as previous commenter mentioned. There is a minor bug in the code above. The last call to CGContextTranslateCTM and CGContextDrawImage should use original width and height instead of roundedRect.size.width and rounded.size.height. Once you do that, this code above works perfectly. Reason - a ctxt with rotated rect is created, origin moved to center, then it is inversely rotated to match original image orientation. At this point you need to move it back in original image's coordinate system.
  • Umair
    Umair about 4 years
    Common solution, but you're rotating ImageView and not the Image itself, which is the original question.