2013-06-04 31 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完全加載之前運行 –