4

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

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

这是代码:

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

    }

}
}

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

4

2 回答 2

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" );
    }
}
于 2013-09-05T01:52:11.487 回答
0
UIImage *image = [UIImage imageNamed:@"myImage"];
    [image drawInRect: destinationRect];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);

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

于 2016-11-18T21:01:36.233 回答