[[[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'm using to save my 'drawingView' to a png file. I found several questions and answers, so I applied them. I set the opaque of my 'drawingView' and drawingView.layer as NO, and set the background color of my 'drawingView' as [UIColor clearColor]. I think I applied all answers from stackoverflow. However, there's nothing changed. The background of png fil is still black. I need transparent background, not black!!
I tried whether there's any problem with UIImage *image1. I used image1 to show on the screen, then I could find black background from that image1. So I could guess there is any problem when creating image1.
This is all I found. Is there any possible solution to save my png image with transparent background image? Thanks in advance!!
An important benefit, and oftentimes deciding factor for using a PNG file, is that–unlike a JPG–they support transparency. This allows you to have a transparent background around an irregular-shaped object and avoid a white (or other colored) box outlining your image.
When a PNG image with a transparent background is selected from the Recent FIle selector and appears with a black background, the black background can be removed by re-uploading the image as a New File each time you use the image.
Oh, god! I did it!! I added [[UIColor clearColor] set]; . 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();
Swift 4.2 version:
extension UIView {
func saveAsImage()-> UIImage? {
UIGraphicsBeginImageContext(self.bounds.size)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
UIColor.clear.set()
context.fill(self.bounds)
self.isOpaque = false
self.layer.isOpaque = false
self.backgroundColor = UIColor.clear
self.layer.backgroundColor = UIColor.clear.cgColor
self.layer.render(in: context)
let image = UIGraphicsGetImageFromCurrentImageContext()
return image
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With