获取网页的图像

时间:2013-04-19 10:46:23

标签: iphone ios objective-c ios5 ios4

我正在尝试捕获网页的完整图像.. 但它显示了空白图像..

我该怎么做才能获得完整的网页图片?

我试过

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

请提出一些解决方案.. WebPageImage是UIWebView ..

4 个答案:

答案 0 :(得分:4)

UIGraphicsBeginImageContextWithOptions(WebPageImage.bounds.size, WebPageImage.opaque, 0.0);
[WebPageImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

答案 1 :(得分:1)

根据您的需要使用此功能。下面的代码将为您提供整个网页的快照

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

答案 2 :(得分:0)

试试这个:

[WebPageImage.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();

答案 3 :(得分:0)

尝试使用我的波纹管方法捕获该图像...

-(IBAction)captureScreen:(id)sender
{   
    UIGraphicsBeginImageContext(webview.frame.size);
    [webview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}

另请参阅此链接的另一个答案.. howe-to-capture-uiview-top-uiview