試圖創建已在我的應用中打開的文檔的縮略圖openurl
。UIWebview僅爲.doc文件捕獲屏幕截圖
我的應用程序可以打開並保存.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>>
爲什麼我不能用上面的代碼保存其他類型文檔的真實內容?
在黑暗中只是一個鏡頭,但也許您的截圖是在webview渲染圖像之前進行的。嘗試在webviewfinishedloading之後幾秒鐘拍攝一個屏幕截圖,看看它是否仍然是白色的。 –
@THE_DOM我現在正在嘗試,但用戶取消/解除視圖,而我推遲了該功能幾秒鐘... –
不是一個永久的解決方案,只是檢查,看看是否發生了什麼。 –