2 for 2012 for_2012 于 2013.05.30 15:11 提问

获取网页整个图片失败

需要捕获网页的全屏图片,但是显示出来是空白。

应该怎么实现?

我的失败代码:

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);
});

1个回答

xinyoulingxi1_tong
xinyoulingxi1_tong   2013.05.30 17:28

根据需求修改吧,这是可以捕捉整个屏幕的抓图

- (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;
}
Csdn user default icon
上传中...
上传图片
插入图片