0

尝试创建已在我的应用程序中打开的文档的缩略图openurl

我的应用程序可以打开和保存 .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>>

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

4

2 回答 2

0

所以我能想到的两种方法如下:

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

2.) 异步截屏,保证webview存在或对异步任务做适当的错误处理,以处理webview不再存在的事件。

我个人更喜欢#2,因为它从主线程中提取了一些繁重的工作并且仍然可以正常工作。

于 2013-06-04T15:02:45.603 回答
0

可能是您用于截屏的方法有问题,请使用 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;
}

更多细节:UIKit 应用程序中的屏幕截图

于 2013-06-04T15:10:11.133 回答