简体   繁体   中英

Xcode iOS Generate .png file and save it using NSFileManager

I am making an iOS app and I got a UIImage - I want to compress it into .png file and save it to the app's documents folder - I already have the path and all I need is how to convert the UIImage to .png and save it.

Thanks, Matan.

so the code is:

UIImage *yourImage = ...; //the image you have
NSString *targetPath = ...; // something like ~/Library/Documents/myImage.png

[UIImagePNGRepresentation(yourImage) writeToFile:targetPath atomically:YES];

For PNG:

UIImagePNGRepresentation

Returns the data for the specified image in PNG format

NSData * UIImagePNGRepresentation (
   UIImage *image
);

If you wanted JPEG instead:

UIImageJPEGRepresentation

Returns the data for the specified image in JPEG format.

NSData * UIImageJPEGRepresentation (
   UIImage *image,
   CGFloat compressionQuality
);

Image compression form is JPEG you can use different quality of jpg image

// for getting png image
 NSData*theImageData=UIImagePNGRepresentation(theImage);
// for JPG compression change fill value between less than 1 
 NSData*theImageData=UIImageJPEGRepresentation(theImage, 1);
// for converting raw data image to UIImage
 UIImage *imageOrignal=[UIImage imageWithData:theImageData];
// for saving in to photos gallery
 UIImageWriteToSavedPhotosAlbum(imageOrignal,nil,nil,nil);

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