0

如何从 UIView 子类创建 CGImage?

我应该使用 renderInContext:UIGraphicsGetCurrentContext() 从包含绘图(通过 drawRect 方法的核心图形)的视图(UIView 的子类)创建 CG 图像吗?

我有一个视图 myView,我正在其中进行一些绘图。我想用我创建的绘图做一些其他的核心图形操作——比如用导入的照片混合模式等,所以我需要从我假设的 myView 中获取一个 CGimage。找不到 CGImage 属性(我的第一个猜测)。四处寻找我找到了下面的解决方案,但是我的子类“没有声明选择器renderInContext”,所以我得到了一个错误。

UIGraphicsBeginImageContext(myView.bounds.size);
[myView renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGImageRef cgImage = image.CGImage;
4

1 回答 1

1

-renderInContext:是一种方法CALayer,不是UIView

改为这样做:

[myView.layer renderInContext:UIGraphicsGetCurrentContext()];

您还需要导入以下标题CALayer

 #import <QuartzCore/QuartzCore.h>

并将您的应用程序与QuartzCore框架链接。

于 2012-07-15T05:13:32.190 回答