如何在iPhone上将图片保存到照片中而不失去质量?

4
使用这种方法保存的图片质量会降低...
UIImage *img=imageview1.image;
UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
4个回答

6

您需要将图像包装在PNG表示中,以便将其保存到照片库中的PNG格式中,而不是JPG格式。

我使用了以下代码,基于Ben Weiss的代码:UIImageWriteToSavedPhotosAlbum saves to wrong size and quality 进行了并排比较。

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

图像将以PNG格式保存在照片库中,以便稍后以完整质量访问/共享。


3

这个函数

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

可以让你获得图像的数据表示,以期望的压缩质量为准(1是最好的)。

然后,您只需将NSData写入文件系统即可获得JPEG文件(例如使用NSData上的writeToURL:atomically:)。

同样的方法也适用于.PNG格式,只需使用UIImagePNGRepresentation即可。

原始文档: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImageJPEGRepresentation


NSData *imageData = UIImagePNGRepresentation(img1); 我通过NSData获取了我的图片。如何保存到照片中? - N Fard

1

我不知道如何在SDK中做到这一点,但是通过UI,如果您将图像复制并粘贴到消息中,则会保留其完整大小。也许SDK中有类似的机制(复制到剪贴板)?不确定。希望它能给你一个想法。


0

使用时:

UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);

压缩质量大约为0.75

image?.jpegData(compressionQuality: 0.75)

自己动手试试,以验证其正确性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接