ios - 在 iOS 中更改图像的打印尺寸

标签 ios uiprintpagerenderer uiprintformatter uiprintinteractioncntrler

在我的应用中,我让用户制作一张优惠券,进行预览,然后他们可以选择打印该优惠券。现在它只是简单地裁剪屏幕以仅包括优惠券部分并进行打印,但它正在打印为全尺寸 8.5 x 11 并且在这样做时使用大量墨水,因为优惠券在背景中是黑色的。如何将其更改为仅打印 say 到 2.5 x 3.5 英寸的纸张部分?

-(void)printer {

    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];

    if(!controller){

        NSLog(@"Couldn't get shared UIPrintInteractionController!");

        return;

    }

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =

    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {

        if(!completed && error){

            NSLog(@"FAILED! due to error in domain %@ with error code %u",

                  error.domain, error.code);

        }

    };
    CGRect rect;
    rect=CGRectMake(0, 0, 320, 480);
    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context=UIGraphicsGetCurrentContext();
    [self.tabBarController.view.layer renderInContext:context];

    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImage *img = [self cropImage:image rect:CGRectMake(20,70,279,359)];

    UIPrintInfo *printInfo = [UIPrintInfo printInfo];

    printInfo.outputType = UIPrintInfoOutputPhoto;

    printInfo.jobName = @"Your Coupon";

    controller.printInfo = printInfo;

    controller.showsPageRange = YES;



    controller.printingItem = img;



    [controller presentAnimated:YES completionHandler:completionHandler];


}

最佳答案

听起来像是将您打印的图像缩放到输出媒体的完整尺寸。您可以尝试将优惠券图像放入更大的图像中并打印该合成图像。我没有测试此代码,但它是我对裁剪图像所做的调整。

UIImage *img = [self cropImage:image rect:CGRectMake(20,70,279,359)];
// Some size for the full image.
// Obviously you need to experiment with this to get the size right
CGSize targetSize = CGSizeMake(800, 1050);

// This will put the image in the upper left corner
// you could put it in the middle or wherever
CGPoint imageOrigin = CGPointMake(0, 0);
CGRect imageRect = CGRectMake(imageOrigin.x, imageOrigin.y, img.size.width, img.size.height);

// Always scale to the screen scale to get the sharpest images even at the expense of size.
UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0);
[img drawInRect:imageRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// print configuration stuff from your code example goes here...

controller.printingItem = newImage;

关于ios - 在 iOS 中更改图像的打印尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21802435/

相关文章:

ios - 无法让 iOS Print Renderer 正确绘制

ios - UIPrintInteractionController 无边框打印

ios - UIPrintInteractionController 无法在 iPhone 中打印整页

ios - Xcode Storyboard属性检查器中的 "Installed"复选框

ios - 增加最后一节和新标题之间的高度

swift - 从 UIWebView 创建 PDF

swift - UIView 的 AirPrint 内容

ios - 如何在终端上从 xcarchive 导出 dsym 文件

ios - 兑换交易,删除 tableviewcell