2013-06-04 32 views
0

试图创建已在我的应用中打开的文档的缩略图openurlUIWebview仅为.doc文件捕获屏幕截图

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

这里是我测试一些样本文档:

.doc.pdfexcel

这里是我的代码:

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

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

+0

在黑暗中只是一个镜头,但也许您的截图是在webview渲染图像之前进行的。尝试在webviewfinishedloading之后几秒钟拍摄一个屏幕截图,看看它是否仍然是白色的。 –

+0

@THE_DOM我现在正在尝试,但用户取消/解除视图,而我推迟了该功能几秒钟... –

+0

不是一个永久的解决方案,只是检查,看看是否发生了什么。 –

回答

0

所以两种方法我能想到的工作了这一点,如下所示:

1)子类的UIWebView并实施的drawRect。在drawRect中保存截图,因为您可以直接访问graphicsContext。在您有一个屏幕截图后,将其分配给webview上的自定义属性。然后,只要你需要它,它应该是随时可用的。

2.)异步截图并确保webview存在或对异步任务进行适当的错误处理以处理webview不再存在的事件。

我个人比较喜欢#2,因为它会让主线脱离一些沉重的部分,并且仍然可以正常工作。

0

也许这与你使用采取截图,使用由苹果指定的一个梅索德一个问题:

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

更多细节:Screen Capture in UIKit Applications

+0

方法工作正常,因为它可以采取屏幕(我的意思是屏幕截图不是窗口大小它的web视图大小)为.doc文件拍摄,问题是方法在webview完全加载之前运行 –