简体   繁体   中英

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

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.

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

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.

When using:

UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);

The compression quality is about 0.75

image?.jpegData(compressionQuality: 0.75)

Try it yourself for proof. 🙂

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