Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

objective c - Printing UIImage using AirPrint causes cut-off content

I am printing out a UIImage using AirPrint but the margins aren't correct. This is what it looks like when it prints:
Photo of printout

Is there a way I can make it fit perfectly on the paper?

Here's the code as requested:

    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    //pic.delegate = del;

    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = [NSString stringWithFormat:@"New Ticket"];
    pic.printInfo = printInfo;


    pic.printingItem = [UIImage imageWithContentsOfFile:path];

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if (!completed && error) {
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error." 
                                                   message:[NSString stringWithFormat:@"An error occured while printing: %@", error]
                                                  delegate:nil 
                                         cancelButtonTitle:@"OK" 
                                         otherButtonTitles:nil, nil];

            [av show];
            [av release];
        }
    };

    [pic presentAnimated:YES completionHandler:completionHandler];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your best bet is probably to scale the image down to the ratio of the printed image, that way you can see the whole thing on the page.

There are several ways of doing this, so I will just tell you the way that I would do it. I maintain a class called ANImageBitmapRep that has several different types of image scaling. The kind of scaling that you want keeps the entire image, but centers it inside of a rectangle of a certain size. First, you must add the ANImageBitmapRep source files to your project, and #import ANImageBitmapRep.h:

#import "ANImageBitmapRep.h"

Then, scale the image like this:

pic.printingItem = [[UIImage imageWithContentsOfFile:path] imageFittingFrame:CGSizeMake(478, 640)];

You can change the CGSizeMake(x, y) to whatever you want, in order to adjust the scale and resolution of the scaled image.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...