如何获取未呈现的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(); return snapshot; } 

它不起作用,快照是一个空白的UIImage因为snapshotView没有呈现。

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

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

那么,我怎样才能把snapshotView的快照(UIImage)不渲染呢?

drawViewHierarchyInRect将为你工作。 你可以直接在你的WKWebView上使用它。

这个苹果技术问答有一些有用的细节。 我在这里也回答一个稍微不同的问题。

我在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 

我不知道你是什么意思的“明显的方法” – 但我已经testing了这个WKWebView,它的工作原理。