0

でアプリで開いたドキュメントのサムネイル画像を作成しようとしていますopenurl

私のアプリは .doc .xls および .pdf ファイルを開いて保存できますが、スクリーンショットを保存しようとすると、.doc コンテンツを正しく保存できます。pdf および xls の場合、空白の白い画像しか保存されません。

ここに私がテストしているいくつかのサンプルドキュメントがあります:

.doc.pdfエクセル

これが私のコードです:

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

おそらく、スクリーンショットを撮るために使用している方法に問題がある可能性があります。

- (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 に答える
0

したがって、これを解決するために私が考えることができる2つの方法は次のとおりです。

1.) UIWebview をサブクラス化し、drawRect を実装します。グラフィックスコンテキストに直接アクセスできるので、drawRect でスクリーンショットを保存します。スクリーンショットを取得したら、それを webview のカスタム プロパティに割り当てます。そうすれば、いつでも必要なときにすぐに利用できるはずです。

2.) スクリーンショットを非同期で取得し、webview が存在することを保証するか、非同期タスクで適切なエラー処理を実行して、webview が存在しないというイベントを処理します。

個人的には #2 を好みます。これは、メイン スレッドから重い負荷を取り除き、適切に動作するはずだからです。

于 2013-06-04T15:02:45.603 に答える