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