Reputation: 4561
I want to take image from documents directory then rotate and reflect the changes to the same image file in documents directory.
I am getting image by using imagepath like this
[UIImage imageWithContentsOfFile:imgURL];
I want to rotate this image by 90 degrees and save at the same path with same image name.
Upvotes: 1
Views: 1337
Reputation: 3928
Try this code:
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);
//------Now writte image data to file --------
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];
Upvotes: 1
Reputation: 1286
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];
Upvotes: 1