Nick Turner
Nick Turner

Reputation: 989

Image not rotating correctly

rotateI am trying to rotate my images by 90 degrees. It works without the rotate, but when I rotate and translate it doesn't show. What am i doing wrong?

            NSString *filePath = [[NSBundle mainBundle] pathForResource:@"check" ofType:@"jpg"];
UIImage *newImage = [[UIImage alloc] initWithContentsOfFile:filePath];

UIGraphicsBeginImageContext(newImage.size);
//CGContextRef c = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(UIGraphicsGetCurrentContext(), newImage.size.width, 0);
//CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, newImage.size.width);
//CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
CGContextRotateCTM(UIGraphicsGetCurrentContext(), 90);
CGRect imageRect = CGRectMake(0, 0, newImage.size.height, newImage.size.width);   
CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, newImage.CGImage);
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

ImageView.image = viewImage;

Upvotes: 0

Views: 278

Answers (3)

Nick Turner
Nick Turner

Reputation: 989

The problem was my define statement and I needed to flip the image. Here is the completed code: ( The image has to be perfect because an OCR process the image and if it's even a little off, it gets rejected.

    CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSaveGState(c);

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"check" ofType:@"jpg"];
UIImage *newImage = [[UIImage alloc] initWithContentsOfFile:filePath];

NSLog(@"Rect width: %@", NSStringFromCGSize(newImage.size) );
UIGraphicsBeginImageContext(newImage.size);
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, newImage.size.width);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);

CGContextTranslateCTM( UIGraphicsGetCurrentContext(), 0.5f * newImage.size.width, 0.5f * newImage.size.height ) ;
CGContextRotateCTM( UIGraphicsGetCurrentContext(), radians( -90 ) ) ;
    CGRect imageRect = CGRectMake(-(newImage.size.height* 0.5f) + 200,  -(0.5f * newImage.size.width) + 200 , newImage.size.height - 200, newImage.size.width - 200);
CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, newImage.CGImage);

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
ImageView.image = viewImage;

CGContextRestoreGState(c);

Upvotes: 0

JP Illanes
JP Illanes

Reputation: 3675

You should do:

UIGraphicsBeginImageContext(newImage.size);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM (context, 90* M_PI/180); //Degrees should be in radians.

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

UIImage *viewImage =  UIGraphicsGetImageFromCurrentImageContext();

Good luck.

UPDATE:

Actually, this is a duplicate from How to Rotate a UIImage 90 degrees?

Upvotes: 0

Yuliani Noriega
Yuliani Noriega

Reputation: 1083

I would suggest using image view and use it's transform like so:

UIImage *newImage = [[UIImage alloc] initWithContentsOfFile:filePath];
UIImageView *newImageView = [[UIImageView alloc] initWithImage:newImage]; 
newImageView.transform = CGAffineTransformMakeRotation(M_PI_2); 

Upvotes: 2

Related Questions