user2115160
user2115160

Reputation:

Rotating UIImageView data -

Hi in my iOS application I want to rotate the UIImage data in 90 Degrees.

I used the below code for 180 degree rotation - and it works fine.

- (UIImage*)upsideDownBunny:(UIImage *)img {

    CGSize imgSize = [img size];
    UIGraphicsBeginImageContext(imgSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextRotateCTM(context,  M_PI);
    CGContextTranslateCTM(context, -imgSize.width, -imgSize.height);
    [img drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

But when I try to rotate in 90 degree it is not working - please correct me

- (UIImage *)upsideDownBunny:(UIImage *)img {

    CGSize imgSize = [img size];
    UIGraphicsBeginImageContext(imgSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextRotateCTM(context,  M_PI_2);
    CGContextTranslateCTM(context, -imgSize.width, -imgSize.height);
    [img drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

Upvotes: 1

Views: 94

Answers (2)

Navnath Godse
Navnath Godse

Reputation: 2225

Try this

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

    // Rotate in degrees
    CGFloat degrees = M_PI_2;

    // Calculate the size of the rotated view's containing box for our drawing space
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    CGAffineTransform transform = CGAffineTransformMakeRotation(degrees);
    rotatedViewBox.transform = transform;
    CGSize rotatedSize = rotatedViewBox.frame.size;

    // Create image context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image.
    CGContextTranslateCTM(context, rotatedSize.width * 0.5f, rotatedSize.height * 0.5f);

    // Rotate the image context
    CGContextRotateCTM(context, degrees);

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

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

    return newImage;
}

The problem is your context is being rotated around (0,0) which is the top left corner and after 90 degrees rotate it will go out of bounds. So you need to move the origin of the context to middle.

Upvotes: 0

Keshav
Keshav

Reputation: 3283

Try this...

UIImage * LandscapeImage = [UIImage imageNamed: imgname];
UIImage * PortraitImage = [[UIImage alloc] initWithCGImage: LandscapeImage.CGImage
                                                 scale: 1.0
                                           orientation: UIImageOrientationLeft];

hope this helps you!

Upvotes: 1

Related Questions