【问题标题】:Converting a CGLayer to a *transparent* UIImage and PNG file in CoreGraphics iphone在 CoreGraphics iphone 中将 CGLayer 转换为 *transparent* UIImage 和 PNG 文件
【发布时间】:2009-07-09 00:26:33
【问题描述】:

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

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

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

这仅使用视图来创建 UIImage。

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

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

任何想法将不胜感激。

  • 阿利姆

【问题讨论】:

    标签: iphone uiimage core-graphics


    【解决方案1】:

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

    self.backgroundColor = [UIColor clearColor];
    

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

    【讨论】:

    • 我也卡在这一点上。能否请您更详细地解释您的答案。或者请提供示例源代码
    • 我不确定您需要什么信息。您能否进一步解释您遇到困难的地方?
    • UIGraphicsBeginImageContext(myDrawView.bounds.size) myDrawView.drawViewHierarchyInRect(myDrawView.bounds, afterScreenUpdates: true) let img = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil) 它不会将其保存为 PNG ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2012-12-22
    • 2014-05-05
    相关资源
    最近更新 更多