2012-11-13 111 views
0

旋转我试图旋转我的图像90度。它工作时没有旋转,但是当我旋转并翻译它不显示。我究竟做错了什么?图像旋转不正确

  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; 
+0

支票本的其他问题http://stackoverflow.com/questions。/10307521/IOS-PNG-图像旋转90度 – tkanzakic

+0

CGContextRotateCTM使用角度以弧度..所以M_PI_2为90度。 –

回答

0

重复的问题是我的定义语句,我需要翻转图像。下面是完整的代码:(图像必须是完美的,因为OCR处理图像,如果它甚至有点过,但被拒绝

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); 
2

我会建议使用图像查看和使用它的变换就像这样:

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

因为这种图像具有要发送到服务器用于图像授权我不能使用的图像视图 –

+0

你总是可以直接使用'newImageView.image'从imageview中访问图像 –

0

你应该这样做:

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(); 

好运。

UPDATE:

其实,这是从How to Rotate a UIImage 90 degrees?