如何拍摄未渲染的UIView的快照? [英] How can I take a snapshot of a UIView that isn't rendered?

查看:135
本文介绍了如何拍摄未渲染的UIView的快照?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此方法拍摄快照:

  UIView * snapshotView = [someView snapshotAfterScreenUpdates:NO]; 

这给了我一个可以玩的UIView。



但是,我需要它的UIImage而不是UIView。



这是我用来将UIView转换为UIImage的方法:

   - (UIImage *)snapshotOfView:(UIView *)view 
{
UIImage * snapshot;

UIGraphicsBeginImageContext(view.frame.size);

[view.layer renderInContext:UIGraphicsGetCurrentContext()];

snapshot = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

返回快照;
}

它不起作用,快照是空白的UIImage,因为snapshotView不是没有渲染。



显而易见的事情是直接拍摄视图的快照,而不是以UIView的形式拍摄快照然后转换它。 / p>

显而易见的方法的问题是我使用的WKWebView有一个巨大的错误,不允许你截取屏幕截图。这是真的,我甚至报告了这个错误,他们说他们正试图解决它。



那么,我怎样才能拍摄快照(UIImage) snapshotView没有渲染吗?

解决方案

drawViewHierarchyInRect 将适合你。您可以直接在WKWebView上使用它。



Apple Technical Q& A 。另外,我在这里处理了一个稍微不同的问题。



我在UIView的类别中使用它:

  @implementation UIView(ImageSnapshot)
- (UIImage * )imageSnapshot {
UIGraphicsBeginImageContextWithOptions(self.bounds.size,
YES,self.contentScaleFactor);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
返回newImage;
}
@end

我不知道你的意思'显而易见的方法' - 但我已经在WKWebView上测试了它并且它可以工作。


I use this method for taking a snapshot:

UIView *snapshotView = [someView snapshotAfterScreenUpdates:NO];

This gives me a UIView that I can play with.

But, I need the UIImage of it and not the UIView.

This is the method I use for converting a UIView to an UIImage:

- (UIImage *)snapshotOfView:(UIView *)view
{
    UIImage *snapshot;

    UIGraphicsBeginImageContext(view.frame.size);

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    snapshot = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return snapshot;
}

It doesn't work, the snapshot is a blank UIImage because the snapshotView isn't rendered.

The obvious thing to do is to directly take a snapshot of the view instead of taking the snapshot in the form of a UIView and then converting it.

The problem with the obvious method is that I'm using a WKWebView that has a huge bug that doesn't allow you to take screenshots. And that's true, I even reported the bug and they said they are trying to fix it.

So, how can I take the snapshot (UIImage) of the snapshotView without rendering it?

解决方案

drawViewHierarchyInRect will work for you. You can use this directly on your WKWebView.

There is some useful detail on this Apple Technical Q&A. Also I touch on it here in answer to a slightly different question.

I use it in a category on UIView:

@implementation UIView (ImageSnapshot)
- (UIImage*)imageSnapshot {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, 
                                    YES, self.contentScaleFactor);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
@end

I don't know what you mean by 'the obvious method' - but I have tested this on a WKWebView and it works.

这篇关于如何拍摄未渲染的UIView的快照?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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