0

Trying to create thumb pictures of documents that has been opened in my app with openurl.

My app can open and save .doc .xls and .pdf file, but when I try to save the screenshot it can correctly save the .doc content, for pdf and xls it only saves a blank white picture.

here are some sample documents I am testing:

.doc, .pdf, excel

Here is my code:

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

Why I cant save real content for other types of documents with above code?

SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
  • Just a shot in the dark, but maybe your screenshot is being taken before the webview has rendered the image. try taking a screen shot a couple of seconds after the webviewfinishedloading and see if it is still white. – THE_DOM Jun 04 '13 at 14:48
  • @THE_DOM I was just trying that right now, but What it user cancels/dismisses that view while I am delaying that function for few seconds... – SpaceDust__ Jun 04 '13 at 14:55
  • Not a permanent solution, just a check to see if that is what is happening. – THE_DOM Jun 04 '13 at 15:02
  • @THE_DOM yes delay makes it working but I am still concerned about what if use cancel before delay function start, I dont want to lock main thread for few seconds to prevent user from canceling either. Webview did load should have work – SpaceDust__ Jun 04 '13 at 18:44

2 Answers2

0

So two ways I can think of working this out are as follows:

1.) Subclass UIWebview and implement drawRect. in drawRect save a screenshot as you have direct access to the graphicsContext. After you have a screenshot assign it to a custom property on the webview. Then whenever you need it, it should be readily available.

2.) take screenshot asynchronously and guarantee that the webview will exist or do proper error handling on the asynch task to handle the event that the webview no longer exists.

I personally would prefer #2 as it pulls some heavy lifting off the main thread and should still work properly.

THE_DOM
  • 4,266
  • 1
  • 18
  • 18
0

Perhaps it's a problem with the methode you are using to take the screenShot, use the one indicated by 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;
}

For more detail : Screen Capture in UIKit Applications

samir
  • 4,501
  • 6
  • 49
  • 76
  • method is working fine because it can take screen(what i mean by screen shot is not window size it webview size ) shot for .doc files, problem is methods runs before webview is fully loaded – SpaceDust__ Jun 04 '13 at 18:46