4

I want to rotate an UIImage (not UIImageView) in custom degree I followed this post but it didn't work for me.

Anyone can help? Thanks.

UPDATE: The code below does some of the job, but I lose some of the image after rotating it:

enter image description here

What should I change to get it right? (btw the yellow color in the screenshots is my UIImageView bg)

- (UIImage *) rotate: (UIImage *) image
{
    double angle = 20;
    CGSize s = {image.size.width, image.size.height};
    UIGraphicsBeginImageContext(s);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, 0,image.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);

    CGContextRotateCTM(ctx, 2*M_PI*angle/360);
    CGContextDrawImage(ctx,CGRectMake(0,0,image.size.width, image.size.height),image.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}
Community
  • 1
  • 1
deaa
  • 41
  • 1
  • 1
  • 2

5 Answers5

8

This method return you image on your angle of rotate

#pragma mark -
#pragma mark Rotate Image 

- (UIImage *)scaleAndRotateImage:(UIImage *)image  { 

    CGImageRef imgRef = image.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);

    CGFloat boundHeight;

    boundHeight = bounds.size.height;  
    bounds.size.height = bounds.size.width; 
    bounds.size.width = boundHeight;
    transform = CGAffineTransformMakeScale(-1.0, 1.0);
    transform = CGAffineTransformRotate(transform, M_PI / 2.0); //use angle/360 *MPI

    UIGraphicsBeginImageContext(bounds.size);   
    CGContextRef context = UIGraphicsGetCurrentContext();   
    CGContextConcatCTM(context, transform); 
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageCopy;   

}
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
GhostRider
  • 1,197
  • 10
  • 19
3
- (UIImage *)rotate:(UIImage *)image radians:(float)rads
{
    float newSide = MAX([image size].width, [image size].height);
    CGSize size =  CGSizeMake(newSide, newSide);
    UIGraphicsBeginImageContext(size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, newSide/2, newSide/2);
    CGContextRotateCTM(ctx, rads);
    CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(-[image size].width/2,-[image size].height/2,size.width, size.height),image.CGImage);
    //CGContextTranslateCTM(ctx, [image size].width/2, [image size].height/2);

    UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return i;
}

this function rotates any image on its center, but the image becomes a square so I would suggest referencing the image center when drawing it after this function.

2

You need to address two things to make this work.

  1. You are rotating about the bottom corner of the image instead of the centre
  2. The bounding rectangle of the resulting image needs to be larger now the image is rotated for it to fit in.

To solve the rotation about the centre, first perform a translate to the centre, then rotate, then translate back.

CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2);
transform = CGAffineTransformRotate(transform, angle);
transform = CGAffineTransformScale(transform, 1.0, -1.0);

CGContextConcatCTM(context, transform);

// Draw the image into the context
CGContextDrawImage(context, CGRectMake(-imageView.image.size.width/2, -imageView.image.size.height/2, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage);

// Get an image from the context
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];

To calculate the size of the bounding rectangle you'd need to fit the new rotated image into use this:

- (CGRect) getBoundingRectAfterRotation: (CGRect) rectangle byAngle: (CGFloat) angleOfRotation {
    // Calculate the width and height of the bounding rectangle using basic trig
    CGFloat newWidth = rectangle.size.width * fabs(cosf(angleOfRotation)) + rectangle.size.height * fabs(sinf(angleOfRotation));
    CGFloat newHeight = rectangle.size.height * fabs(cosf(angleOfRotation)) + rectangle.size.width * fabs(sinf(angleOfRotation));

    // Calculate the position of the origin
    CGFloat newX = rectangle.origin.x + ((rectangle.size.width - newWidth) / 2);
    CGFloat newY = rectangle.origin.y + ((rectangle.size.height - newHeight) / 2);

    // Return the rectangle
    return CGRectMake(newX, newY, newWidth, newHeight);
}

You can find these techniques in my previous posts and answers here:

Creating a UIImage from a rotated UIImageView

and here:

Saving 2 UIImages

Hope this helps,

Dave

Community
  • 1
  • 1
Magic Bullet Dave
  • 9,006
  • 10
  • 51
  • 81
  • Hi, this code does not work with photos that has different orientation than UIImageOrientationUp, do you have any solution? – Pion Apr 12 '13 at 16:18
0

for rotate image.. you can use this IBAction ... for each and every button click, the image will be rotate by 90 degree...

-(IBAction)rotateImageClick:(id)sender{

UIImage *image2=[[UIImage alloc]init];
image2 = [self imageRotatedByDegrees:self.roateImageView.image deg:(90)]; //Angle by 90 degree
self.roateImageView.image = image2;
imgData= UIImageJPEGRepresentation(image2,0.9f);

}

for rotating image u only have to pass UIimage and rotating degrees for the following method

- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees

//------------------------------------------------------------------------

#pragma mark - imageRotatedByDegrees Method

- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{

    // calculate the size of the rotated view's containing box for our drawing space
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)];

    CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);
    rotatedViewBox.transform = t;

    CGSize rotatedSize = rotatedViewBox.frame.size;
    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    //   // Rotate the image context
    CGContextRotateCTM(bitmap, (degrees * M_PI / 180));

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

I think this link will be help.......! Rotate Original Image by clicking button in Objective C

http://adrianmobileapplication.blogspot.com/2015/03/rotate-original-image-by-clicking.html

S.D.Adrian
  • 11
  • 3
-3

You have to do some thing like this

YourContainer.transform = CGAffineTransformMakeRotation( 270.0/180*M_PI );

I think rest of the thing you can figured out..

Sat
  • 1,616
  • 3
  • 22
  • 40