我正在尝试捕获webview正在向用户显示的图像,因此我可以对网页进行一些颜色分析.当我试图从它的父母那里获取图像时,我基本上得到一个白色的盒子,即使页面已呈现:
func makeImageSnapshot()-> (NSImage)
{
let imgSize = self.view.bounds.size
let bir = self.viewbitmapImageRepForCachingDisplayInRect(self.webView!.view.bounds)
bir.size = imgSize
self.webView.cacheDisplayInRect(self.view.bounds, toBitmapImageRep:bir)
let image = NSImage(size:imgSize)
image.addRepresentation(bir)
self.image = image
return image
}
func saveSnapshot()
{
let imgRep = self.image!.representations[0]
let data = imgRep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: nil)
data.writeToFile("/tmp/file.png", atomically: false)
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我无法访问webView内部的实际视图(在本例中为边界)的属性.当我尝试访问它时,编译器barfs:
/Users/josh/Canary/MacOsCanary/canary/canary/Modules/Overview/Overview.swift:55:37:'(NSView!,stringForToolTip:NSToolTipTag,point:NSPoint,userData:UnsafePointer <()>) - > String! " 没有名为'bounds'的成员
我的猜测是,由于OS X和iOS使用的扩展方法,这种情况正在发生.任何想法,还是我应该回到使用旧版WebView?
我已经尝试过将屏幕外渲染WKWebView成图像
func cacheDisplayInRect(rect: NSRect, toBitmapImageRep bitmapImageRep: NSBitmapImageRep)func drawLayer(layer: CALayer, inContext ctx: CGContext)没有成功.生成的图像始终为空(白色或透明).有没有人设法在优胜美地上做到这一点?
我正在尝试捕获WKWebView的屏幕快照,但是我的方法无法正常工作,它返回纯色,好像图层树为空,而它似乎可以在其他视图上工作。
- (UIImage *)screenshot
{
UIImage *screenshot;
UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenshot;
}
Run Code Online (Sandbox Code Playgroud) 我相信NDA会下降,所以我可以问这个问题.我有一个UIView子类:
BlurView *blurredView = ((BlurView *)[self.view snapshotViewAfterScreenUpdates:NO]);
blurredView.frame = self.view.frame;
[self.view addSubview:blurredView];
Run Code Online (Sandbox Code Playgroud)
它到目前为止在捕获屏幕方面做了它的工作,但现在我想模糊那个视图.我到底该怎么做?根据我的阅读,我需要捕获视图的当前内容(上下文?!)并将其转换为CIImage(不?),然后将CIGaussianBlur应用于它并将其绘制回视图.
我到底该怎么做?
PS视图没有动画,所以它应该是性能明智的.
编辑:这是我到目前为止.问题是我无法将快照捕获到UIImage,我得到一个黑屏.但是如果我直接将视图添加为子视图,我可以看到快照就在那里.
// Snapshot
UIView *view = [self.view snapshotViewAfterScreenUpdates:NO];
// Convert to UIImage
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Apply the UIImage to a UIImageView
BlurView *blurredView = [[BlurView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
[self.view addSubview:blurredView];
blurredView.imageView.image = img;
// Black screen -.-
Run Code Online (Sandbox Code Playgroud)
BlurView.m:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.imageView = [[UIImageView alloc] …Run Code Online (Sandbox Code Playgroud)