Matan
Matan

Reputation: 456

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.

Upvotes: 0

Views: 3712

Answers (3)

Wain
Wain

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

DreamWatcher
DreamWatcher

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

Daij-Djan
Daij-Djan

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

Related Questions