0

谁能指出一些示例代码来捕获嵌入在 iphone 应用程序中的完整 web 视图的图像?webview 的高度取决于内容,因此简单的屏幕截图不会对用户有用,但我想在导航栏中给他们一个按钮,用于捕获全高图像并将其保存到他们的照片图书馆。

提前致谢!

4

2 回答 2

0

如上UIWebView呈现的内容UIScrollView,您可以从上到下开始截屏,通过更改contentOffset的参数UIScrollView,并加入截屏。并且,记得检查contentSize. 祝你好运!

于 2013-10-16T12:58:18.810 回答
-2

使用此代码:

    - (IBAction)buttonPressed:(id)sender 
{
    webViewHeight = [[self.myWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] integerValue];

CGRect screenRect = self.myWebView.frame;

double currentWebViewHeight = webViewHeight;
while (currentWebViewHeight > 0)
{
    imageName ++;

    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);

    [self.myWebView.layer renderInContext:ctx];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",imageName]];

    if(currentWebViewHeight < 960)
    {
        CGRect lastImageRect = CGRectMake(0, 960 - currentWebViewHeight, self.myWebView.frame.size.width, currentWebViewHeight);
        CGImageRef lastImageRef = CGImageCreateWithImageInRect([newImage CGImage], lastImageRect);                
        newImage = [UIImage imageWithCGImage:lastImageRef]; 
        CGImageRelease(lastImageRef);
    }

    [UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES];

    [self.myWebView stringByEvaluatingJavaScriptFromString:@"window.scrollBy(0,960);"];
    currentWebViewHeight -= 960;
 }
 }
于 2013-10-16T12:50:55.460 回答