透明度问题 - 将 UIView 转换为 UIImage 时
我想从 UIView
创建一个 UIImage
对象。该视图不是不透明的。 为此,我使用以下代码:
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
但由于某种原因,我得到的是黑色背景的图像,而不是透明背景。 我已经尝试将 UIGraphicsBeginImageContextWithOptions 中的“opaque”参数设置为 NO,但没有任何不同的结果。
有什么建议吗?
(类似的问题CGContext透明度问题没有答案)
I want to create an UIImage
object from a UIView
. The view is NOT opaque.
for that i'm using the following code:
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
But for some reason instead of transparent background i'm getting a image with black background.
I already tried to set the "opaque" parameter in UIGraphicsBeginImageContextWithOptions
to NO
but with no different results.
Any suggestion?
(Similar question CGContext transparency problem with no answer)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚遇到了同样的问题。对我有用的是确保我使用的是 PNG 而不是 JPG,因为 JPG 不支持 Alpha 通道。希望这对你有用。
i just came across this same problem. what worked for me was assuring that i was working with PNGs and not JPGs, since JPGs do not support alpha channel. hope this works for you.
除非我遗漏了什么,否则我相信通过将 opaque 设置为 NO 可以得到你想要的结果
Unless I'm missing something, I believe you get the result you want by setting opaque to NO
我很确定在创建这样的 UIImage 时您无法定义透明度 - 您可以掩盖使用图像的 UIImageView 。
类似下面的函数允许这种类型的操作:
您可以这样调用它:
masked_image.png 需要是一个 alpha 通道图像,它将剪掉您的黑色背景。
I'm pretty sure you can't define a transparency when creating UIImages like this - you could mask your UIImageView which uses the image though.
Something like the following functions allow for this type of operation:
You can than call it like this:
masked_image.png needs to be an alpha channeled image which will cut out your black bg.