我想从文档目录中获取图像,然后旋转并将更改反映到文档目录中的同一图像文件。
我通过像这样使用图像路径来获取图像
[UIImage imageWithContentsOfFile:imgURL];
我想将此图像旋转 90 度并以相同的图像名称保存在相同的路径中。
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];
试试这个代码:
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];