2011-05-19 54 views
1

如何輕鬆地旋轉保存在文檔目錄中的圖像文件?如何旋轉圖像文件?

我的代碼保存它:

(...) 
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext(); 
NSData* imageData = UIImageJPEGRepresentation(capturedImage, 1.0); 
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
[imageData writeToFile:[docDir stringByAppendingPathComponent:@"saved.jpg"] atomically:NO]; 
UIGraphicsEndImageContext(); 

回答

2

輕鬆使用CGContextRotateCTM()實現:

發現在一個SO答案在這裏:How to Rotate a UIImage 90 degrees?

static inline double radians (double degrees) {return degrees * M_PI/180;} 
- (UIImage*) rotateImage:(UIImage*)src rotationDirection:(UIImageOrientation)orientation { 
    UIGraphicsBeginImageContext(src.size); 

    CGContextRef context(UIGraphicsGetCurrentContext()); 
    if (orientation == UIImageOrientationRight) { 
     CGContextRotateCTM (context, radians(90)); 
    } else if (orientation == UIImageOrientationLeft) { 
     CGContextRotateCTM (context, radians(-90)); 
    } else if (orientation == UIImageOrientationDown) { 
     // NOTHING 
    } else if (orientation == UIImageOrientationUp) { 
     CGContextRotateCTM (context, radians(90)); 
    } 

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

    return UIGraphicsGetImageFromCurrentImageContext(); 
} 
+0

這甚至不是一個真正的的OBJ -C方法..我嘗試了一些東西,但它並沒有在這種情況下旋轉任何東西。 – yosh 2011-05-20 09:33:47

+0

這是一個容易調用的函數。但是,如果你喜歡一種方法,沒問題。我已經改變了代碼來反映這一點。 – memmons 2011-05-22 14:59:43

+0

對不起,我之前正在檢查類似的功能代碼,並有多個語法錯誤。我之前嘗試過CGContextRotateCTM,但沒有做任何事情,稍後會嘗試解決您的問題,謝謝! – yosh 2011-05-23 08:55:26