2012-04-19 2 views
4

현재 에어 프린트를 통해 뷰 내용을 인쇄 할 수 있습니다. 이 기능을 사용하려면보기에서 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을 구현 결국 다음 destinationRect이 축소 버전의 크기에 따라 크기가됩니다

- (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); 

.

관련 문제