我目前正在研究通过 Airprint 打印视图内容的可能性。对于这个功能,我从视图中创建了一个 UIImage 并将它发送到 UIPrintInteractionController。
问题是图像的大小被调整为纸张的全分辨率,而不是原始大小(大约 300x500 像素)。有谁知道如何从我的图像创建一个合适的页面。
这是代码:
/** Create UIImage from UIScrollView**/
-(UIImage*)printScreen{
UIImage* img = nil;
UIGraphicsBeginImageContext(scrollView.contentSize);
{
CGPoint savedContentOffset = scrollView.contentOffset;
CGRect savedFrame = scrollView.frame;
scrollView.contentOffset = CGPointZero;
scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
scrollView.backgroundColor = [UIColor whiteColor];
[scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
img = UIGraphicsGetImageFromCurrentImageContext();
scrollView.contentOffset = savedContentOffset;
scrollView.frame = savedFrame;
scrollView.backgroundColor = [UIColor clearColor];
}
UIGraphicsEndImageContext();
return img;
}
/** Print view content via AirPrint **/
-(void)doPrint{
if ([UIPrintInteractionController isPrintingAvailable])
{
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
UIImage *image = [(ReservationOverView*)self.view printScreen];
NSData *myData = [NSData dataWithData:UIImagePNGRepresentation(image)];
if(pic && [UIPrintInteractionController canPrintData: myData] ) {
pic.delegate =(id<UIPrintInteractionControllerDelegate>) self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputPhoto;
printInfo.jobName = [NSString stringWithFormat:@"Reservation-%@",self.reservation.reservationID];
printInfo.duplex = UIPrintInfoDuplexNone;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = myData;
//pic.delegate = self;
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);
}
};
[pic presentAnimated:YES completionHandler:completionHandler];
}
}
}
我试图手动调整图像大小,但这不能正常工作。