objective-c - 使用 AirPrint 打印 UIImage 导致内容被截断

标签 objective-c ios cocoa-touch airprint

我正在使用 AirPrint 打印出 UIImage,但页边距不正确。这是打印时的样子:
Photo of printout

有没有办法让它完美地贴合在纸上?

这是请求的代码:

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

最佳答案

最好的办法可能是将图像按打印图像的比例缩小,这样您就可以在页面上看到整个内容。

有几种方法可以做到这一点,所以我只告诉你我会做的方法。我维护一个名为 ANImageBitmapRep 的类有几种不同类型的图像缩放。您想要的缩放比例保留整个图像,但将其居中放置在特定大小的矩形内。首先,您必须将 ANImageBitmapRep 源文件添加到您的项目中,然后#import ANImageBitmapRep.h:

#import "ANImageBitmapRep.h"

然后,像这样缩放图像:

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

您可以将 CGSizeMake(x, y) 更改为您想要的任何值,以调整缩放图像的比例和分辨率。

关于objective-c - 使用 AirPrint 打印 UIImage 导致内容被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7652082/

相关文章:

ios - 将UIView/图形上下文划分为多个区域

ios - iOS:13位置始终允许:点击“打开设置”后,它不会重定向到“应用程序设置”页面

ios - 为什么 Xcode 自动完成在导入时建议 .m 文件?

iphone - 以编程方式使 iOS 设备静音/静音?

objective-c - UIGestureRecognizer State Changed 和 touchesMoved 之间有什么区别?

objective-c - 在 iPhone 的 UITextField 中验证电子邮件地址

ios - Apple设备中的Google Analytics(分析)跟踪问题

ios - 如何让我的应用程序与锁定屏幕上的媒体控制按钮一起使用?

ios - sizeWithFont 返回随机值

iphone - 应用程序本身的应用程序名称