简体   繁体   中英

How to rotate image by 90 degrees and save

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.

  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];

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM