2012-07-07 71 views
5
[[[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的背景仍然是黑色的。我需要透明背景,而不是黑色!将UIView保存为透明背景的png文件

我试过UIImage * image1是否有问题。我使用image1在屏幕上显示,然后我可以从该图像中找到黑色背景1。所以我可以猜测创建image1时有任何问题。

这是我找到的。有没有任何可能的解决方案,以透明的背景图像保存我的PNG图像?提前致谢!!

回答

8

天啊!我做的!!我添加了[[UIColor clearColor]集]; 。就这样。

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();