Printing UIImage using AirPrint causes cut-off content

半城伤御伤魂 提交于 2019-11-28 07:45:07

问题


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

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];

回答1:


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.



来源:https://stackoverflow.com/questions/7652082/printing-uiimage-using-airprint-causes-cut-off-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!