ios 如何从当前图形上下文创建 UIImage?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1112947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a UIImage from the current graphics context?
提问by figelwump
I'd like to create a UIImage object from the current graphics context. More specifically, my use case is a view that the user can draw lines on. They may draw incrementally. After they are done, I'd like to create a UIImage to represent their drawing.
我想从当前图形上下文创建一个 UIImage 对象。更具体地说,我的用例是用户可以在其上画线的视图。他们可能会逐步绘制。完成后,我想创建一个 UIImage 来表示他们的绘图。
Here is what drawRect: looks like for me now:
这是 drawRect: 现在对我来说的样子:
- (void)drawRect:(CGRect)rect
{
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSaveGState(c);
CGContextSetStrokeColorWithColor(c, [UIColor blackColor].CGColor);
CGContextSetLineWidth(c,1.5f);
for(CFIndex i = 0; i < CFArrayGetCount(_pathArray); i++)
{
CGPathRef path = CFArrayGetValueAtIndex(_pathArray, i);
CGContextAddPath(c, path);
}
CGContextStrokePath(c);
CGContextRestoreGState(c);
}
... where _pathArray is of type CFArrayRef, and is populated every time touchesEnded: is invoked. Also note that drawRect: may be invoked several times, as the user draws.
... 其中 _pathArray 是 CFArrayRef 类型,并且每次 touchesEnded: 被调用时都会填充。还要注意 drawRect: 可能会在用户绘制时被调用多次。
When the user is done, I'd like to create a UIImage object that represents the graphics context. Any suggestions on how to do this?
当用户完成后,我想创建一个代表图形上下文的 UIImage 对象。关于如何做到这一点的任何建议?
回答by Kelvin
You need to setup the graphics context first:
您需要先设置图形上下文:
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
回答by Ben Gotow
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
If you need to keep image
around, be sure to retain it!
如果您需要保留image
,请务必保留它!
EDIT: If you want to save the output of drawRect to an image, just create a bitmap context using UIGraphicsBeginImageContext
and call your drawRect function with the new context bound. It's easier to do than saving the CGContextRef you're working with within drawRect - because that context may not have bitmap information associated with it.
编辑:如果要将 drawRect 的输出保存到图像,只需使用创建位图上下文UIGraphicsBeginImageContext
并使用新的上下文绑定调用 drawRect 函数。这比在 drawRect 中保存您正在使用的 CGContextRef 更容易 - 因为该上下文可能没有与之关联的位图信息。
UIGraphicsBeginImageContext(view.bounds.size);
[view drawRect: [myView bounds]];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
You can also use the approach that Kelvin mentioned. If you want to create an image from a more complex view like UIWebView, his method is faster. Drawing the view's layer does not require refreshing the layer, it just requires moving image data from one buffer to another!
您也可以使用 Kelvin 提到的方法。如果你想从像 UIWebView 这样更复杂的视图创建图像,他的方法更快。绘制视图的图层不需要刷新图层,它只需要将图像数据从一个缓冲区移动到另一个缓冲区!
回答by Sahil Kapoor
Swift Version
迅捷版
func createImage(from view: UIView) -> UIImage {
UIGraphicsBeginImageContext(view.bounds.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let viewImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return viewImage
}