将UIView保存为透明PNG。

16

我有一个UIView,想将其保存为透明的PNG图片,即没有UIView背景颜色...

目前我正在使用这段代码,它可以正常工作但带着背景颜色 :(

UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image1);
[imageData writeToFile:filePath atomically:YES];

有没有人知道如何将这张图片转换成透明的呢?

先谢谢了。


1
尝试将视图的backgroundColor设置为[UIColor clearColor]; - jsadfeew
2
谢谢您的回复,问题是我想让用户看到背景,并且我想在应用程序运行时生成图像并将其存储在文档中 :( - user416445
你成功过了吗?我需要做类似的事情。 - Travis
1
你可以将透明视图作为具有背景颜色的视图的子视图添加,但只有透明视图会被渲染成PNG。 - Pascal
1
如果你和我一样,来到这里是为了找出如何将UIView转换为png,请注意你必须导入<QuartzCore/QuartzCore.h>。 - audub
4个回答

10

在我的情况下,我忘记设置不透明属性。它应该被设置为NO:

view.opaque = NO;

这就是缺失的一环!我使用了这个(https://dev59.com/s2855IYBdhLWcg3wcz_y#22494886)答案来保存图片。 - srik

7
将背景颜色更改为 [UIColor clear],绘制图像,然后将背景颜色设置回原始颜色。由于GUI更新仅在下一个运行循环周期中触发,因此用户不应看到任何闪烁。
UIColor* color=self.backgroundColor;
view.backgroundColor=[UIColor clearColor];

UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image1);
[imageData writeToFile:filePath atomically:YES];

self.backgroundColor=color;

3
作为对Gilad回答的补充,在视网膜显示器上可能会导致一些质量问题。要获取视网膜上下文,您可以使用这篇帖子中的代码。
UIColor* color=self.backgroundColor;
view.backgroundColor=[UIColor clearColor];

// This is for retina render check
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(YourView.frame.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(keyWindow.bounds.size);
// And it goes up to here the rest stays the same

[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image1);
[imageData writeToFile:filePath atomically:YES];

self.backgroundColor=color;

0

对于Objective-C,您必须将opaque设置为NO

 + (UIImage *) imageWithView:(UIView *)view 
    {     
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [[UIScreen mainScreen] scale]);     
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];     
        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();     
        UIGraphicsEndImageContext();     
        return img; 
    }

对于Swift,您需要将opaque = false设置

func imageWithView(inView: UIView) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(inView.bounds.size, false, 0.0)
        if let context = UIGraphicsGetCurrentContext() {
            inView.layer.render(in: context)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
        return nil
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接