gpt4 book ai didi

ios - 将 UIView 保存为具有透明背景的 png 文件

转载 作者:可可西里 更新时间:2023-11-01 04:23:57 24 4
gpt4 key购买 nike

[[[globalSingleton paintingView] drawingView] setOpaque:NO];
[[[[globalSingleton paintingView] drawingView] layer] setOpaque:NO];
[[[globalSingleton paintingView] drawingView] setBackgroundColor:[UIColor clearColor]];
[[[[globalSingleton paintingView] drawingView] layer] setBackgroundColor:[UIColor clearColor].CGColor];

[[[[globalSingleton paintingView] drawingView] layer] renderInContext:ctx];

UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();

上面的代码是我用来将“drawingView”保存到 png 文件的代码。我找到了几个问题和答案,所以我应用了它们。我将“drawingView”和 drawingView.layer 的不透明设置为 NO,并将“drawingView”的背景颜色设置为 [UIColor clearColor]。我想我应用了 stackoverflow 的所有答案。但是,没有任何改变。 png fil 的背景仍然是黑色。我需要透明背景,而不是黑色!!

我试了下UIImage *image1 有没有问题。我使用 image1 在屏幕上显示,然后我可以从该 image1 中找到黑色背景。所以我可以猜测在创建 image1 时有什么问题。

这就是我找到的全部。是否有任何可能的解决方案来保存我的带有透明背景图像的 png 图像?提前致谢!!

最佳答案

天哪!我做的!!我添加了 [[UIColor clearColor] set]; .就这样。

UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, [[UIScreen mainScreen] scale]);

CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor clearColor] set];
CGContextFillRect(ctx, screenRect);

[[[globalSingleton paintingView] drawingView] setOpaque:NO];
[[[[globalSingleton paintingView] drawingView] layer] setOpaque:NO];
[[[globalSingleton paintingView] drawingView] setBackgroundColor:[UIColor clearColor]];
[[[[globalSingleton paintingView] drawingView] layer] setBackgroundColor:[UIColor clearColor].CGColor];

[[[[globalSingleton paintingView] drawingView] layer] renderInContext:ctx];

UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();

关于ios - 将 UIView 保存为具有透明背景的 png 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11378165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com