2012-04-19 6 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

答えて

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は、小型化バージョンの大きさに応じて大きさになります。

関連する問題