Monday, 3 September 2012

For the printing of image view or image in iPhone

For the printing of image view or image in iPhone
 - (IBAction)printContent  {
        // Obtain the shared UIPrintInteractionController
        UIPrintInteractionController *controller1 = [UIPrintInteractionController sharedPrintController];
        if(!controller1){
            NSLog(@"Couldn't get shared UIPrintInteractionController!");
            return;
        }
       
        // We need a completion handler block for printing.
        UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
            if(completed && error)
                NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
        };
       
        // Obtain a printInfo so that we can set our printing defaults.
        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        UIImage *image = [UIImage imageNamed:@"mainimage1.jpg"];
       
        // This application prints photos. UIKit will pick a paper size and print
        // quality appropriate for this content type.
        printInfo.outputType = UIPrintInfoOutputPhoto;
        // The path to the image may or may not be a good name for our print job
        // but that's all we've got.
        printInfo.jobName = [[imageURL path] lastPathComponent];
       
        // If we are performing drawing of our image for printing we will print
        // landscape photos in a landscape orientation.
        if(!controller1.printingItem && image.size.width > image.size.height)
            printInfo.orientation = UIPrintInfoOrientationLandscape;
       
        // Use this printInfo for this print job.
        controller1.printInfo = printInfo;
       
        //  Since the code below relies on printingItem being zero if it hasn't
        //  already been set, this code sets it to nil.
        controller1.printingItem = nil;
       
       
#if DIRECT_SUBMISSION
        // Use the URL of the image asset.
        if(imageURL && [UIPrintInteractionController canPrintURL:imageURL])
            controller1.printingItem = imageURL;
#endif
       
        // If we aren't doing direct submission of the image or for some reason we don't
        // have an ALAsset or URL for our image, we'll draw it instead.
        if(!controller1.printingItem){
            // Create an instance of our PrintPhotoPageRenderer class for use as the
            // printPageRenderer for the print job.
            PrintPhotoPageRenderer *pageRenderer = [[PrintPhotoPageRenderer alloc]init];
            // The PrintPhotoPageRenderer subclass needs the image to draw. If we were taking
            // this path we use the original image and not the fullScreenImage we obtained from
            // the ALAssetRepresentation.
            pageRenderer.imageToPrint = image;
            controller1.printPageRenderer = pageRenderer;
            [pageRenderer release];
        }
       
        // The method we use presenting the printing UI depends on the type of
        // UI idiom that is currently executing. Once we invoke one of these methods
        // to present the printing UI, our application's direct involvement in printing
        // is complete. Our delegate methods (if any) and page renderer methods (if any)
        // are invoked by UIKit.
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            //[controller presentFromBarButtonItem:self.printButton animated:YES completionHandler:completionHandler];  // iPad
        }else
            [controller1 presentAnimated:YES completionHandler:completionHandler];  // iPhone
       
    }

No comments:

Post a Comment