问题描述
是否可以从使用 snapshotViewAfterScreenUpdates
创建的UIView获取UIImage?
Is it possible to get a UIImage from a UIView created with snapshotViewAfterScreenUpdates
?
从 snapshotViewAfterScreenUpdates
返回的UIView在作为子视图添加时看起来很好,但以下内容会产生黑色图像:
A UIView returned from snapshotViewAfterScreenUpdates
looks fine when added as a subview, but the following produces a black image:
UIView *snapshotView = [someView snapshotViewAfterScreenUpdates:YES];
UIImage *snapshotImage = [self imageFromView:snapshotView];
- (UIImage *)imageFromView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
// [view.layer renderInContext:UIGraphicsGetCurrentContext()]; // <- same result...
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
当然可以在不依赖<$ c $的情况下获取图像c> snapshotViewAfterScreenUpdates :
UIImage *snapshotImage = [self imageFromView:someView];
不幸的是,在捕获复杂视图时, drawViewHierarchyInRect
无法匹配 snapshotViewAfterScreenUpdates
的速度。我希望从 snapshotViewAfterScreenUpdates
创建的视图中获取 UIImage
会更快,这可能吗?
Unfortunately, when capturing a complex view, drawViewHierarchyInRect
can't match the speed of snapshotViewAfterScreenUpdates
. I was hoping that it would be faster to get the UIImage
from a view created by snapshotViewAfterScreenUpdates
, is this possible?
推荐答案
答案似乎是 NO 和Apple的隐含地证实了这一点:
The answer seems to be NO and Apple's documentation implicitly confirms this:
值得注意的是实施参与用户界面在WWDC13上的iOS 会话中列出 snapshotViewAfterScreenUpdates
作为最快的方法,但在示例代码中使用 drawViewHierarchyInRect
。
It's worth noting that the Implementing Engaging UI on iOS session from WWDC13 lists snapshotViewAfterScreenUpdates
as the fastest method, but uses drawViewHierarchyInRect
in the example code.
这篇关于使用snapshotViewAfterScreenUpdates创建的UIView的UIImage:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!