Ios UIWebview仅捕获.doc文件的屏幕截图

Ios UIWebview仅捕获.doc文件的屏幕截图,ios,objective-c,uiwebview,uigraphicscontext,Ios,Objective C,Uiwebview,Uigraphicscontext,正在尝试使用openurl创建已在我的应用程序中打开的文档的拇指图片 我的应用程序可以打开并保存.doc.xls和.pdf文件,但当我尝试保存屏幕截图时,它可以正确保存.doc内容,对于pdf和xls,它只保存一张空白的白色图片 以下是我正在测试的一些示例文档: ,, , 这是我的密码: -(void)webViewDidFinishLoad:(UIWebView *)webViewCapture { [UIApplication sharedApplication].networkA

正在尝试使用
openurl
创建已在我的应用程序中打开的文档的拇指图片

我的应用程序可以打开并保存.doc.xls和.pdf文件,但当我尝试保存屏幕截图时,它可以正确保存.doc内容,对于pdf和xls,它只保存一张空白的白色图片

以下是我正在测试的一些示例文档:

,, ,

这是我的密码:

-(void)webViewDidFinishLoad:(UIWebView *)webViewCapture
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSLog(@"webViewDidFinishLoad");
    NSLog(@"webview %@",webView);
    NSLog(@"webViewCapture %@",webViewCapture);

    UIGraphicsBeginImageContext(webView.bounds.size);
    [webView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Convert UIImage to JPEG
    NSData *imgData = UIImageJPEGRepresentation(viewImage, 1);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePathLocal = [documentsDirectory stringByAppendingPathComponent:@"Inbox"];

    NSArray *partialDates =[self.imageFilename componentsSeparatedByString:@"."];//split string where - chars are found
    NSString* fileDescription= [partialDates objectAtIndex: 0];

    //creatre unique name for image _AKIAIVLFQKM11111
    NSString *uniqueFileName=[NSString stringWithFormat:@"%@_AKIAIVLFQKM11111_%@.jpeg",fileDescription,[partialDates objectAtIndex: 1]];

    NSString *finalFileName= [filePathLocal stringByAppendingPathComponent:uniqueFileName];

    NSError *error = nil;
    if ([imgData writeToFile:finalFileName options:NSDataWritingAtomic error:&error]) {
        // file saved
    } else {
        // error writing file
        NSLog(@"Unable to write PDF to %@. Error: %@", finalFileName, error);
    }

}
NSLOG:

webViewDidFinishLoad
webview <UIWebView: 0xaa79070; frame = (0 44; 768 960); autoresize = W+H; layer = <CALayer: 0xaa51000>>
webViewCapture <UIWebView: 0xaa79070; frame = (0 44; 768 960); autoresize = W+H; layer = <CALayer: 0xaa51000>>
webViewDidFinishLoad
网络视图
webViewCapture

为什么我不能用上述代码为其他类型的文档保存真实内容?

因此,我可以考虑以下两种方法来解决这个问题:

1.)子类UIWebview并实现drawRect。在drawRect中保存屏幕截图,因为您可以直接访问graphicsContext。获得屏幕截图后,将其指定给webview上的自定义属性。那么无论何时你需要它,它都应该随时可用

2.)异步截图,并确保webview将存在,或对异步任务执行正确的错误处理,以处理webview不再存在的事件


我个人更喜欢#2,因为它可以从主线程上卸下一些重物,并且仍然可以正常工作。

也许这是您拍摄屏幕截图的方法有问题,请使用apple指定的方法:

- (UIImage*)screenshot 
{
    // Create a graphics context with the target size
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    else
        UIGraphicsBeginImageContext(imageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Iterate over every window from back to front
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) 
    {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
        {
            // -renderInContext: renders in the coordinate space of the layer,
            // so we must first apply the layer's geometry to the graphics context
            CGContextSaveGState(context);
            // Center the context around the window's anchor point
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            // Apply the window's transform about the anchor point
            CGContextConcatCTM(context, [window transform]);
            // Offset by the portion of the bounds left of and above the anchor point
            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);

            // Render the layer hierarchy to the current context
            [[window layer] renderInContext:context];

            // Restore the context
            CGContextRestoreGState(context);
        }
    }

    // Retrieve the screenshot image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

有关更多详细信息:

只需在黑暗中拍摄,但您的屏幕截图可能是在webview渲染图像之前拍摄的。在WebViews加载完成几秒钟后,尝试拍摄一张屏幕截图,看看它是否仍然是白色的。@我现在只是在尝试,但当我将该功能延迟几秒钟时,it用户取消/取消了该视图…这不是一个永久的解决方案,只是检查一下是否发生了这种情况。@DOM yes delay使其工作,但我仍然担心如果在delay函数启动之前使用cancel,会发生什么,我不想锁定主线程几秒钟,以防止用户取消。Webview没有加载应该有workmethod工作正常,因为它可以为.doc文件拍摄屏幕(我所说的屏幕快照不是窗口大小而是Webview大小),问题是方法在Webview完全加载之前运行