2011-05-19 51 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