Converting a CGLayer to a *transparent* UIImage and PNG file in CoreGraphics iphone

两盒软妹~` 提交于 2019-12-06 14:01:31

问题


I've been making a simple painting app for the iphone. I'm trying to convert the drawn image to a transparent PNG (I don't want a white background). I'd also like to get a UIImage representation to use in a UIImageView.

Currently, I respond to touch events and draw paths to a CGLayer which is then drawn to my views context. I have access to both the CGLayer as well as the view itself. Currently, I output my view to an image using:

UIGraphicsBeginImageContext(drawingView.bounds.size);
[drawingView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();   
UIGraphicsEndImageContext();

This only uses the view to create a UIImage.

Since the view has a white background it is inluded in the UIImage I create. I'd like to get a UIImage that does not have this white background so I can display and write to a PNG file.

I think I should use the CGLayer I have directly but I'm not sure how to get that from the CGLayerRef type I have access to.

Any ideas would be much appreciated.

  • Aleem

回答1:


I just dealt with this. Don't draw a background on the CGLayer and then set the background of the UIView to:

self.backgroundColor = [UIColor clearColor];

That should do it and your png will have a transparent background.



来源:https://stackoverflow.com/questions/1101254/converting-a-cglayer-to-a-transparent-uiimage-and-png-file-in-coregraphics-iph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!