使用这种方法保存的图片质量会降低...
UIImage *img=imageview1.image;
UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
UIImage *img=imageview1.image;
UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
您需要将图像包装在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格式保存在照片库中,以便稍后以完整质量访问/共享。
这个函数
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
可以让你获得图像的数据表示,以期望的压缩质量为准(1是最好的)。
然后,您只需将NSData写入文件系统即可获得JPEG文件(例如使用NSData上的writeToURL:atomically:
)。
同样的方法也适用于.PNG格式,只需使用UIImagePNGRepresentation
即可。
我不知道如何在SDK中做到这一点,但是通过UI,如果您将图像复制并粘贴到消息中,则会保留其完整大小。也许SDK中有类似的机制(复制到剪贴板)?不确定。希望它能给你一个想法。
使用时:
UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
压缩质量大约为0.75
image?.jpegData(compressionQuality: 0.75)
自己动手试试,以验证其正确性。