gpt4 book ai didi

uiview - UIImage 来自使用 snapshotViewAfterScreenUpdates : 创建的 UIView

转载 作者:行者123 更新时间:2023-12-03 10:42:48 25 4
gpt4 key购买 nike

是否可以从使用 snapshotViewAfterScreenUpdates 创建的 UIView 获取 UIImage ?

snapshotViewAfterScreenUpdates 返回的 UIView添加为 subview 时看起来不错,但以下内容会生成黑色图像:

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];

不幸的是,在捕获复杂 View 时, drawViewHierarchyInRect跟不上 snapshotViewAfterScreenUpdates的速度.我希望能更快地获得 UIImage来自 snapshotViewAfterScreenUpdates 创建的 View , 这可能吗?

最佳答案

答案似乎是和苹果的 documentation暗中证实了这一点:

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



值得注意的是,WWDC13 中的在 iOS session 上实现引人入胜的 UI 列出了 snapshotViewAfterScreenUpdates作为最快的方法,但使用 drawViewHierarchyInRect在示例代码中。

关于uiview - UIImage 来自使用 snapshotViewAfterScreenUpdates : 创建的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20203682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com