在CoreGraphics iphone中将CGLayer转换为*透明* UIImage和PNG文件 [英] Converting a CGLayer to a *transparent* UIImage and PNG file in CoreGraphics iphone

查看:122
本文介绍了在CoreGraphics iphone中将CGLayer转换为*透明* UIImage和PNG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为iphone制作一个简单的绘画应用程序。我正在尝试将绘制的图像转换为透明的PNG(我不需要白色背景)。我还想获得一个UIImage表示形式,以在UIImageView中使用。

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.

当前,我响应触摸事件并绘制到CGLayer的路径,然后将其绘制到我的CGLayer上。查看上下文。我可以访问CGLayer以及视图本身。目前,我使用以下方法将视图输出到图像:

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

这仅使用视图创建UIImage。

This only uses the view to create a UIImage.

由于视图具有白色背景,因此包含在我创建的UIImage中。我想要一个没有白色背景的UIImage,以便可以显示和写入PNG文件。

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.

我想我应该直接使用我拥有的CGLayer,但是我不确定如何从 CGLayerRef 输入我可以访问的类型。

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

推荐答案

我刚刚处理了这个问题。不要在 CGLayer 上绘制背景,然后将 UIView 的背景设置为:

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];

应该这样做,您的png将具有透明背景。

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

这篇关于在CoreGraphics iphone中将CGLayer转换为*透明* UIImage和PNG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆