2011年6月27日 星期一

利用AirPrint列印UIWebView當前顯示的範圍

// Get the UIWebView content and convert to UIImage
UIViewPrintFormatter* printFormatter = [_webView viewPrintFormatter];
UIGraphicsBeginImageContext(_webView.frame.size);
[printFormatter.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// For Testing check the view layer size
//[UIImagePNGRepresentation(newImage) writeToFile:@"/Users/kito/Desktop/testfile.png" atomically:TRUE];

// Assign the AirPrint Job
UIPrintInteractionController* printController = [UIPrintInteractionController sharedPrintController];
printController.printingItem = newImage;

// Present the AirPrint Dialog from Bar Button
[printController presentFromBarButtonItem:barBtn animated:TRUE completionHandler:nil];

沒有留言:

張貼留言