问题描述
- 获取网页整个图片失败
-
需要捕获网页的全屏图片,但是显示出来是空白。应该怎么实现?
我的失败代码:
CGSize overallSize = [WebPageImage sizeThatFits:CGSizeZero]; UIGraphicsBeginImageContext(WebPageImage.scrollView.contentSize); WebPageImage.bounds = CGRectMake(0, 0, overallSize.width, overallSize.height); while (WebPageImage.loading) { [NSThread sleepForTimeInterval:0.1]; } UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,(unsigned long)NULL), ^(void) { UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); });
解决方案
根据需求修改吧,这是可以捕捉整个屏幕的抓图
- (UIImage *) imageFromWebView:(UIWebView *)view
{
// tempframe to reset view size after image was created
CGRect tmpFrame = view.frame;
// set new Frame
CGRect aFrame = view.frame;
aFrame.size.height = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].height;
aFrame.size.width = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].width;
view.frame = aFrame;
// do image magic
UIGraphicsBeginImageContext([view sizeThatFits:[[UIScreen mainScreen] bounds].size]);
CGContextRef resizedContext = UIGraphicsGetCurrentContext();
[self.thmbWebView.layer renderInContext:resizedContext];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// reset Frame of view to origin
view.frame = tmpFrame;
return image;
}
时间: 2024-10-07 18:54:36