4

私は現在、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];

    }

}
}

画像のサイズを手動で変更しようとしましたが、正しく機能しません。

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 に答える