我有一个 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/