我创建了一个CALayer (我向该图层添加了几个形状,但最终只有一个CALayer),现在我想将它添加到一个CGImage中,但是我遇到了很多麻烦。我找到了一些关于[CALayer renderInContext:ctx]的参考资料,但我不确定如何实现它。有人以前做过这个吗?
试试这个...UIGraphicsBeginImageContext(layer.bounds.size); [layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
UIGraphics*Context
函数只能在主线程中运行(所以它们在任何操作系统版本上都不是线程安全的)。CGBitmapContextCreate
是线程安全的 CoreGraphics 函数,而UIGraphicsBeginImageContext
就是基于它构建的。 - rpetrich