为UIPrintInteractionController调整UIImage的大小

时间:2012-04-19 15:11:09

标签: iphone uiimage airprint

我目前正致力于通过Airprint打印视图内容。 对于这个功能,我正在从视图创建一个UIImage并将其发送到UIPrintInteractionController。

问题是图像的大小调整为纸张的全分辨率,而不是原始尺寸(约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];

    }

}
}

我尝试手动调整图片大小,但这无法正常工作。

2 个答案:

答案 0 :(得分:1)

我在Apple上找到了这个示例代码:

  

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" );
    }
}

答案 1 :(得分:0)

UIImage *image = [UIImage imageNamed:@"myImage"];
    [image drawInRect: destinationRect];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);

destinationRect将根据缩小版本的尺寸进行调整。