在我的应用程序中,我让用户制作一张优惠券,预览它,然后他们可以选择打印该优惠券。现在它只是缩小屏幕以仅包含优惠券部分并打印,但它打印到全尺寸 8.5 x 11 并且使用大量墨水,因为优惠券在背景中是黑色的。如何将其更改为仅打印 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];
}