Search code examples
iphoneiosipaduiwebviewairprint

Print UIWebView screen


I have a UIWebView with a graphic generated by Google Charts (javascript rendered)

The screen is not a pdf or png.

Is it possible to capture what's on the screen and send it to AirPrint?


Solution

  • Take a screenshot of the webview and send it to AirPrint.

    // TAKE SCREENSHOT
    CGRect myRect = [self.view bounds];
    UIGraphicsBeginImageContext(myRect.size);   
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, myRect);
    [self.view.layer renderInContext:ctx];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    NSData *imageData = UIImageJPEGRepresentation(viewImage, 1.0);
    UIGraphicsEndImageContext();