尝试使用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>>
为什么我无法使用上述代码保存其他类型文档的真实内容?
答案 0 :(得分:0)
我认为有两种方法可以解决这个问题:
1。)子类UIWebview并实现drawRect。在drawRect中保存屏幕截图,因为您可以直接访问graphicsContext。屏幕截图后,将其分配给webview上的自定义属性。然后,只要你需要它,就应该随时可用。
2。)以异步方式截取屏幕截图,并保证webview将存在或对异步任务执行正确的错误处理,以处理webview不再存在的事件。
我个人更喜欢#2,因为它会从主线程中拉出一些重要的东西并且应该仍能正常工作。
答案 1 :(得分:0)
使用screenShot的方法可能会出现问题,请使用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;
}