N Fard
N Fard

Reputation: 1099

how to save Image to Photos in iPhone without losing quality?

the quality of saved image will lose when i'm using this method ...

UIImage *img=imageview1.image;
UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);

Upvotes: 4

Views: 3849

Answers (4)

Michael N
Michael N

Reputation: 559

When using:

UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);

The compression quality is about 0.75

image?.jpegData(compressionQuality: 0.75)

Try it yourself for proof. 🙂

Upvotes: 0

Paul Solt
Paul Solt

Reputation: 8395

You need to wrap the image in a PNG representation, so that it's saved to the Photo Library in PNG format, rather than JPG format.

I used the following code based on the code from Ben Weiss: UIImageWriteToSavedPhotosAlbum saves to wrong size and quality for a side by side comparison.

// Image contains a non-compressed image stored within my Documents directory
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
NSData* imdata = UIImagePNGRepresentation ( image ); 
UIImage* im2 = [UIImage imageWithData:imdata]; 
UIImageWriteToSavedPhotosAlbum(im2, nil, nil, nil); 

The image will be saved in the PNG format in the Photo Library so that it can be accessed/shared later in full quality.

Upvotes: 6

yonel
yonel

Reputation: 7865

The function


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

enables you to get the data representation of your image for the expected compression quality (1 is best).

Then you just have to write your NSData to the file system to get the JPEG file (using writeToURL:atomically: on NSData for instance).

The same can be applied to .PNG with the UIImagePNGRepresentation.

The original doc : http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImageJPEGRepresentation

Upvotes: 3

Trenton
Trenton

Reputation: 11976

I don't know how to do it in the SDK, but though the UI, if you copy + paste the image into a message, it keeps its full size. Perhaps there's some mechanism like that in the SDK (copy to clipboard)? Dunno. Hope it gives you an idea.

Upvotes: 1

Related Questions