为 UIPrintInteractionController 调整 UIImage 的大小 [英] Resize UIImage for UIPrintInteractionController

查看:23
本文介绍了为 UIPrintInteractionController 调整 UIImage 的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究通过 Airprint 打印视图内容的可能性.对于这个功能,我从视图中创建一个 UIImage 并将它发送到 UIPrintInteractionController.

I'm currently working on a possibility to print the content of a view via Airprint. For this feature I'm creating a UIImage from the view and send it to UIPrintInteractionController.

问题是图像被调整为纸张的全分辨率,而不是原始尺寸(约 300x500 像素).有谁知道如何根据我的图片创建一个合适的页面.

The problem is that the image is resized to the full resolution of the paper and not it's original size (approx. 300x500px). Does anybody know how to create a proper page from my image.

代码如下:

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

    }

}
}

我已尝试手动调整图像大小,但无法正常工作.

I've tried to resize the image manually, but this does not work properly.

推荐答案

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

I've found this sample code on 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 并实现:

And it looks like the proper way to size an image for printing (so it doesn't fill the entire page) is to implement your own UIPrintPageRenderer and implement:

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

printableRect 会告诉您纸张的大小,您可以将其缩小到您想要的大小(大概是通过计算一些 DPI).

The printableRect will tell you the size of the paper and you can scale it down to however much you want (presumably by calculating some DPI).

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

Update: I ended up implementing my own 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" );
    }
}

这篇关于为 UIPrintInteractionController 调整 UIImage 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆