2012-04-19 81 views
4

我目前正在研究通过Airprint打印视图内容的可能性。 对于此功能,我从视图创建UIImage并将其发送到UIPrintInteractionController。为UIPrintInteractionController调整UIImage的大小

问题是图像的大小调整为纸张的全分辨率,而不是原始大小(大约300x500px)。有谁知道如何从我的图像创建一个合适的页面。

下面是代码:

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

    } 

} 
} 

我试图手动调整图像大小,但是这并不能正常工作。

+0

我发现了一个不平凡的解决方案,并将图像添加到自己创建的pdf文件中,但我想知道它是否可能没有pdf文件。 – AlexVogel 2012-04-20 08:24:40

回答

1

我发现在苹果此示例代码:

https://developer.apple.com/library/ios/samplecode/PrintPhoto/Listings/Classes_PrintPhotoPageRenderer_m.html#//apple_ref/doc/uid/DTS40010366-Classes_PrintPhotoPageRenderer_m-DontLinkElementID_6

它看起来像有道尺寸打印的图像(所以它不会填满整个页面)为实现自己的UIPrintPageRenderer和实施:

- (void)drawPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)printableRect 

的printableRect会告诉你的纸张大小,您可以将其缩小无论你想要多少(大概是通过计算一些DPI)。

更新:我最终实现我自己的ImagePageRenderer:

- (void)drawPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)printableRect 
{ 
    if(self.image) 
    { 
     CGSize printableAreaSize = printableRect.size; 

     // Apple uses 72dpi by default for printing images. This 
     // renders out the image to be giant. Instead, we should 
     // resize our image to our desired dpi. 
     CGFloat dpiScale = kAppleDPI/self.dpi; 

     CGFloat imageWidth = self.image.size.width * dpiScale; 
     CGFloat imageHeight = self.image.size.height * dpiScale; 

     // scale image if paper is too small 
     BOOL scaleImage = printableAreaSize.width < imageWidth || printableAreaSize.height < imageHeight; 
     if(scaleImage) 
     { 
      CGFloat widthScale = (CGFloat)printableAreaSize.width/imageWidth; 
      CGFloat heightScale = (CGFloat)printableAreaSize.height/imageHeight; 

      // Choose smaller scale so there's no clipping 
      CGFloat scale = widthScale < heightScale ? widthScale : heightScale; 

      imageWidth *= scale; 
      imageHeight *= scale; 
     } 

     // If you want to center vertically, horizontally, or both, 
     // modify the origin below. 

     CGRect destRect = CGRectMake(printableRect.origin.x, 
             printableRect.origin.y, 
             imageWidth, 
             imageHeight); 

     // Use UIKit to draw the image to destRect. 
     [self.image drawInRect:destRect]; 
    } 
    else 
    { 
     NSLog(@"no image to print"); 
    } 
} 
0
UIImage *image = [UIImage imageNamed:@"myImage"]; 
    [image drawInRect: destinationRect]; 
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext(); 
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil); 

的destinationRect将根据的缩小版本,尺寸的大小。