如何将图像旋转90度并保存

时间:2013-11-11 06:36:26

标签: ios uiimage

我想从文档目录中获取图像然后旋转并将更改反映到文档目录中的同一图像文件。

我正在使用像这样的imagepath获取图像

[UIImage imageWithContentsOfFile:imgURL];

我想将此图像旋转90度并使用相同的图像名称保存在同一路径中。

2 个答案:

答案 0 :(得分:1)

  UIImage *image = [UIImage imageWithContentsOfFile:imgURL];
    UIImage *newImage=[image imageRotatedByDegrees:90.0]; // i guess "image" is your orginal image
[[NSFileManager defaultManager] removeItemAtPath:imgURL];
    [UIImageJPEGRepresentation(newImage) writeToFile:imgURL atomically:YES];

答案 1 :(得分:1)

试试这段代码:

UIImageView *starimage=[[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile: imgURL]];
starimage.transform=CGAffineTransformMakeRotation(3.14159265/2 * -1); //For Left rotation

starimage.transform=CGAffineTransformMakeRotation(3.14159265/2 * 1); //For Right rotation

NSData *imagedata=UIImagePNGRepresentation(starimage.image);

// ------现在将图像数据写入文件--------

NSString *fileName = [NSString stringWithFormat:@"test.png"];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSString *saveFileName = fileName;
NSString *documentPath = [saveDirectory stringByAppendingPathComponent:saveFileName];
// [imagedata writeToFile:documentPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
[imagedata writeToFile:documentPath atomically:YES];