将 UIView 保存为透明 PNG
我有一个 UIView,我希望它存储为透明 PNG,即没有 UIVIew 背景颜色...
我目前正在使用此代码,它工作正常,但具有背景颜色:(
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image1);
[imageData writeToFile:filePath atomically:YES];
那么有人知道如何获取这张图片是透明的吗?
提前谢谢你..
I have a UIView and I want it to be stored as a transparent PNG, i.e. without the UIVIew background color...
I am currently using this code and it's working OK but with the background color :(
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image1);
[imageData writeToFile:filePath atomically:YES];
So does anyone have any idea about getting this image as a transparent one?
Thank you in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就我而言,我忘记了不透明的属性。应将其设置为“否”:
In my case, I forgot the opaque property. It should be set to NO:
将背景颜色更改为[UIColorclear],绘制图像然后将背景颜色设置回原始颜色。
由于 GUI 更新仅在下一个运行循环周期触发,因此用户不应看到任何闪烁。
Change the background color to [UIColor clear], draw the image and then set the background color back to the original color.
Since GUI updates will fire only on the next runloop cycle, the user should not see any flickering.
作为吉拉德答案的补充。在视网膜显示屏上,这可能会导致一些质量问题。要获取视网膜上下文,您可以使用这篇帖子中的代码。
As an addition to Gilad's answer. On retina display this may cause some quality issues. To get the retina context you may use this code from this post.
对于 Objective-c,你必须设置 opaque = NO
对于 Swift,你必须设置 opaque = false
For Objective-c you have to set opaque = NO
For Swift you have to set opaque = false