1

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.

Dhaval Bhadania
  • 3,090
  • 1
  • 20
  • 35
Preetam Jadakar
  • 4,479
  • 2
  • 28
  • 58

2 Answers2

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];
Raon
  • 1,266
  • 3
  • 12
  • 25
1

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];
Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38