1

Possible Duplicate:
Need to retain original quality of image while rotating in iPhone sdk

In my app, I can rotate an image by 90 degrees. When I rotate a number of times... it is getting blurred (pixel clarity is missing). I lost the img quality. Can anyone suggest way to implement this.

Here I used a button to rotate the image. The code used for Rotation is:

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

     CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
     UIGraphicsBeginImageContext(rect.size);

     CGContextRef context = UIGraphicsGetCurrentContext();


     if (img.imageOrientation == UIImageOrientationRight) 
     {
          CGContextRotateCTM (context, M_PI_2);
     } 

     else if (img.imageOrientation == UIImageOrientationLeft) 
     {
          CGContextRotateCTM (context, -(M_PI_2));
     }

     else if (img.imageOrientation == UIImageOrientationDown) 
     {
          // NOTHING
     } 

     else if (img.imageOrientation == UIImageOrientationUp) 
     {
         CGContextRotateCTM (context, M_PI_2);
     }

     //CGContextRotateCTM(context, M_PI_2);

     CGContextTranslateCTM(context,0, -(img.size.width));
     [img drawInRect:CGRectMake(0, 0, img.size.height,img.size.width)];


     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
     appDelegate.savedImage=newImage;

     UIGraphicsEndImageContext();
     CGContextRelease(context);
     return newImage;
     }

Thanks in Advance.

Community
  • 1
  • 1
Lakshmi
  • 585
  • 2
  • 13
  • 22
  • 1
    If rotation is the only manipulation that you do, then I would always keep the original and go from there for each future rotation. e.g. Instead of rotating by +7 and +3 and -2 one after the other I would then get back to the original and rotate by +8 only once. For the next rotation I would scrap the rotated image and get back to the original again. – Hermann Klecker Dec 11 '12 at 09:10
  • you have already asked for that: http://stackoverflow.com/questions/13814544/need-to-retain-original-quality-of-image-while-rotating-in-iphone-sdk ; if you need to add new code or text just edit your old question, instead of create a new question... – meronix Dec 11 '12 at 09:40

2 Answers2

0

you can create a copy of the original image changing the orientation like this:

CGImageRef imageRef = [img CGImage];
UIImage *rotatedImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationUp];

you has to put the second line in every of your conditions and change the orientation parameter to the correct value for each case, check the doc for more info

tkanzakic
  • 5,499
  • 16
  • 34
  • 41
0

Rotating at the view level will preserve the bitmap fidelity (namely, it doesn't touch it).

ie:

#define DEG2RAD(d) (M_PI * d / 180.0)
...
_imageView.transform = CGAffineTransformMakeRotation(DEG2RAD(45));

Unfortunately you then need to look at anti-aliasing the rotated image with a inner border. That's already been answered here before.

Hope this helps!

Matt Melton
  • 2,493
  • 19
  • 25