【发布时间】:2013-12-10 19:19:46
【问题描述】:
是否可以从使用snapshotViewAfterScreenUpdates 创建的 UIView 中获取 UIImage?
从snapshotViewAfterScreenUpdates 返回的 UIView 在添加为子视图时看起来不错,但以下会产生黑色图像:
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;
}
当然可以不依赖snapshotViewAfterScreenUpdates获取图片:
UIImage *snapshotImage = [self imageFromView:someView];
很遗憾,在捕获复杂视图时,drawViewHierarchyInRect 的速度无法与snapshotViewAfterScreenUpdates 相提并论。我希望从snapshotViewAfterScreenUpdates 创建的视图中获取UIImage 会更快,这可能吗?
【问题讨论】:
标签: uiview ios7 uiimage screenshot snapshot