iphone - 如何从 UIView 的内容中获取 CGImageRef?

标签 iphone cocoa-touch uikit core-graphics quartz-2d

我有一个 UIView,我在 -drawRect: 中画了一些东西。现在我需要来自 UIView 的图形上下文或位图的 CGImageRef。有没有简单的方法可以做到这一点?

最佳答案

像这样(凭内存输入,因此可能不是 100% 正确):

// Get a UIImage from the view's contents
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, view.contentScaleFactor);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// Convert UIImage to CGImage
CGImageRef cgImage = image.CGImage;

关于iphone - 如何从 UIView 的内容中获取 CGImageRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3777497/

相关文章:

iphone - 重新定义ios方法

javascript - 损坏的响应式设计(仅在 iPhone 上)- 可以使用 javascript 强制执行媒体查询吗?

c++ - 为 arm64 iPhone 编译 xcode 5 失败

iphone - NSSearchPathForDirectoriesInDomains 麻烦(掉头发快)

iphone - 我应该在 iPhone 操作系统中 -drain 或 -release 一个自动释放池吗?

iphone - 如何在用户点击按钮时以编程方式关闭应用程序

iphone - UIKeyboardTypeNamePhonePad 类型的键盘可以在电话模式下启动吗?

ios - 使 UISlider 高度变大?

iphone - 如何通过 TableView 获取索引值来删除核心数据条目

ios - UIView iOS 9 中的碰撞边界路径原点