Save UIView as png file with transparent background

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

The code above is what I use to save my drawing in a png file. I found some questions and answers, so I applied them. I set the opacity of my drawing pattern and drawing picture to NO and set the background color of my drawing to [UIColor clearColor]. I think I applied all the answers from stackoverflow. However, nothing has changed. Background png fil is still black. I need a transparent background, not black!

I tried if there is a problem with UIImage * image1. I used image1 to display on the screen, then I could find a black background from this image1. Therefore, I could guess that when creating image1 there is some kind of problem.

That is all I have found. Is there a possible solution to save my png image with a transparent background image? Thanks in advance!

+5
source share
1 answer

Oh my God! I did it !! I added [[UIColor clearColor]] ;, That's all.

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();
+8
source

All Articles