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

查看:28
本文介绍了来自使用 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;
}

当然可以在不依赖snapshotViewAfterScreenUpdates的情况下获取图像:

It is of course possible to get the image without relying on 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 的 Implementing Engaging UI on 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天全站免登陆