来自CALayer的UIImage - iPhone SDK [英] UIImage from CALayer - iPhone SDK
问题描述
在我的应用程序中,我创建了一个 CALayer
(带有一些子图层 - CALayer由添加为子图层的形状组成)。
In my application, I have created a CALayer
(with a few sublayers - the CALayer is composed of shapes added as sublayers).
我正在尝试创建一个 UIImage
,我将能够上传到服务器(我有这个代码)。
但是,我无法弄清楚如何将 CALayer
添加到 UIImage
。
I am trying to create a UIImage
that I will be able to upload to a server (I have the code for this).
However, I can't figure out how to add the CALayer
to a UIImage
.
这可能吗?任何建议都会非常好,非常感谢。
Is this possible? Any advice would be excellent and very appreciated.
非常感谢,
Brett
Many thanks, Brett
推荐答案
听起来好像要将图层渲染为UIImage。在这种情况下,下面的方法应该可以解决问题。只需将其添加到您的视图或控制器类,或在CALayer上创建一个类别。
Sounds like you want to render your layer into a UIImage. In that case, the method below should do the trick. Just add this to your view or controller class, or create a category on CALayer.
- (UIImage *)imageFromLayer:(CALayer *)layer
{
UIGraphicsBeginImageContextWithOptions(layer.frame.size, NO, 0);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
这篇关于来自CALayer的UIImage - iPhone SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!