iPhone: распечатать все содержимое прокрутки - PullRequest
1 голос
/ 28 февраля 2012

Мне нужна твоя помощь.Я прочитал документацию Apple о печати, однако у меня возникают трудности с пониманием того, как распечатать все содержимое представления прокрутки.До сих пор я знаю, чтобы проверить, доступен ли принтер

//-----------Check for printer-------------------
if ([UIPrintInteractionController isPrintingAvailable]) {
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                                                               target:self 
                                                                               action:@selector(printWebView:)];

    [self.navigationItem setLeftBarButtonItem:barButton animated:NO];
    self.printButton = barButton;
}

. Я был бы чрезвычайно благодарен, если бы кто-то мог предоставить код для фактического задания на печать.Заранее спасибо ...

Ответы [ 2 ]

2 голосов
/ 20 сентября 2013

Чтобы получить скриншот и распечатать

/ * Возьмите снимок экрана и распечатайте * /

-(void)printItem {

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];

if(printController ) {

    printController.delegate = self;

    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"resultado";
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    printController.printInfo = printInfo;
    printController.showsPageRange = YES;
    printController.printingItem = newImage;

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

    [printController presentAnimated:YES completionHandler:completionHandler];

}
}
0 голосов
/ 28 февраля 2012

Рассматривали ли вы создание UIImage (снимок экрана, своего рода) и печать получившегося изображения? Я не знаю о печати, но могу помочь со скриншотом. Обратите внимание, что если ваше прокручиваемое изображение не помещается на экране, вам нужно изменить следующий код, чтобы увеличить границы.

Наслаждайтесь,

Дэмиен

- (UIImage*)screenshot {
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
    UIGraphicsBeginImageContext(imageSize);

CGContextRef context = UIGraphicsGetCurrentContext();

// Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows]) 
{
    if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
    {
        // -renderInContext: renders in the coordinate space of the layer,
        // so we must first apply the layer's geometry to the graphics context
        CGContextSaveGState(context);
        // Center the context around the window's anchor point
        CGContextTranslateCTM(context, [window center].x, [window center].y);
        // Apply the window's transform about the anchor point
        CGContextConcatCTM(context, [window transform]);
        // Offset by the portion of the bounds left of and above the anchor point
        CGContextTranslateCTM(context,
                              -[window bounds].size.width * [[window layer] anchorPoint].x,
                              -[window bounds].size.height * [[window layer] anchorPoint].y);

        // Render the layer hierarchy to the current context
        [[window layer] renderInContext:context];

        // Restore the context
        CGContextRestoreGState(context);
    }
}

// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;
}

Примечание: я получил этот код из учебника. Это работает (у меня есть в поставляемом коде), но я не понимаю каждую строку.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...