2012-11-13 131 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?