UIImageWriteToSavedPhotosAlbum保存到错误的大小和质量

wuw*_*ngy 8 iphone

我正在尝试拍摄我的应用当前视图的屏幕截图并将其保存到相册(然后通过电子邮件发送或彩信).

UIGraphicsBeginImageContext(self.view.bounds.size); 

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
Run Code Online (Sandbox Code Playgroud)

这可以工作,但是当我从照片库发送电子邮件时,生成的图像会变得更大(533x800像素)并且会被大量压缩.

我已经尝试首先将UIImage写入文件然后保存到相册但仍然遇到同样的问题.

如果我在iPhone上使用内置的屏幕截图功能,则视图会以320x480正确保存到相册,但上述代码似乎会因某种原因保存更大的图像?

谢谢!

小智 20

我发现了一个不错的解决方法,即基本上UIImage将其重新包装为PNG,然后保存重新打包的版本.代码看起来像这样:

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
Run Code Online (Sandbox Code Playgroud)


小智 1

我也遇到了同样的错误,当我将小数点四舍五入到与 iPhone 相同的比例时,这个错误得到了解决,尝试一下,确保比例是 1.0、2.0 等,而不是 3.1,这会导致错误。