开发者

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

开发者 https://www.devze.com 2022-12-22 02:51 出处:网络
the quality of saved image will lo开发者_如何学Csewhen i\'m using this method ... UIImage *img=imageview1.image;

the quality of saved image will lo开发者_如何学Cse 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消