2012-07-07 56 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();