UIImageWriteToSavedPhotosAlbum保存到错误的大小和质量 [英] UIImageWriteToSavedPhotosAlbum saves to wrong size and quality
问题描述
我正在尝试拍摄我应用当前视图的屏幕截图并将其保存到相册(然后通过电子邮件发送或彩信)。
I'm trying to take a screen shot of my app's current view and save it to photo album (to then be emailed or MMS'ed).
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
这是有效的,但是当我从中发送电子邮件时,生成的图像会变大(533x800px)并且压缩很大照片库。
This works but the resulting image apears to be larger (533x800px) and heavily compressed when I email it from the photo library.
我先尝试将UIImage写入文件,然后保存到相册,但仍然遇到同样的问题。
I've tried first writing the UIImage to file and then saving to album but still get the same issue.
如果我在iPhone上使用内置的屏幕截图功能,视图会以320x480正确保存到相册,但上述代码似乎会因某种原因保存更大的图像?
If I used the in-built screenshot functionality on the iPhone the view saves correctly to photo album at 320x480 but the above code appears to save a larger image for some reason?
谢谢!
推荐答案
我找到了一个不错的解决方法,这是基本上将 UIImage
重新包装为PNG,然后保存重新包装的版本。代码看起来像这样:
I found a decent workaround, which is to essentially rewrap the UIImage
as a PNG, then save the rewrapped version. Code looks something like this:
UIImage* im = [UIImage imageWithCGImage:myCGRef]; // make image from CGRef
NSData* imdata = UIImagePNGRepresentation ( im ); // get PNG representation
UIImage* im2 = [UIImage imageWithData:imdata]; // wrap UIImage around PNG representation
UIImageWriteToSavedPhotosAlbum(im2, nil, nil, nil); // save to photo album
这篇关于UIImageWriteToSavedPhotosAlbum保存到错误的大小和质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!