Save UIView to a transparent PNG

user416445 picture user416445 · Jan 19, 2011 · Viewed 8.4k times · Source

I have a UIView and I want it to be stored as a transparent PNG, i.e. without the UIVIew background color...

I am currently using this code and it's working OK but with the background color :(

UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image1);
[imageData writeToFile:filePath atomically:YES];

So does anyone have any idea about getting this image as a transparent one?

Thank you in advance..

Answer

Lukas Kalinski picture Lukas Kalinski · May 5, 2014

In my case, I forgot the opaque property. It should be set to NO:

view.opaque = NO;