Reputation: 456
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.
Upvotes: 0
Views: 3712
Reputation: 119031
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
);
Upvotes: 3
Reputation: 399
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);
Upvotes: 0
Reputation: 50089
so the code is:
UIImage *yourImage = ...; //the image you have
NSString *targetPath = ...; // something like ~/Library/Documents/myImage.png
[UIImagePNGRepresentation(yourImage) writeToFile:targetPath atomically:YES];
Upvotes: 5