Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'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!!

like image 347
Ricky Park Avatar asked Jul 07 '12 19:07

Ricky Park


People also ask

Does PNG allow for transparency?

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.

How do I remove a black background from a PNG?

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.


2 Answers

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();
like image 88
Ricky Park Avatar answered Oct 19 '22 21:10

Ricky Park


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
}
}
like image 5
Vikash Kumar Avatar answered Oct 19 '22 22:10

Vikash Kumar