使用snapshotViewAfterScreenUpdates创建的UIView的UIImage: [英] UIImage from a UIView created with snapshotViewAfterScreenUpdates:

查看:1983
本文介绍了使用snapshotViewAfterScreenUpdates创建的UIView的UIImage:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从使用 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的 documentation 隐含地证实了这一点:

The answer seems to be NO and Apple's documentation implicitly confirms this:


如果要对快照应用图形效果(如模糊),请使用 drawViewHierarchyInRect: afterScreenUpdates:方法
而不是。

If you want to apply a graphical effect, such as blur, to a snapshot, use the drawViewHierarchyInRect:afterScreenUpdates: method instead.

值得注意的是实施参与用户界面在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:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆