Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customise printing with UIActivityViewController

I've been using UIPrintInteractionController to print images in my app and it's worked out fine, since I can customise the page so that it prints with the appropriate margins.

I'm attempting to make use of the UIActivityViewController to do the same, however I've come across a bit of a block - whenever I pass in a UIImage in the activityItems, the print activity becomes available, however when it prints there are no margins and I'm not sure how to customise it. I've attempted to use UIMarkupTextPrintFormatter, however it then prints the image out over two pages and it's oversized, so part of the image is cut off the page. UIPrintInteractionController seemed to resize it appropriately when I passed in the image in the form of an NSData object.

This is what I've attempted to pass in the activityItems:

 NSString *markupText = [NSString stringWithFormat:@"<html><body><img src='%@'></body></html>",self.imgURL]; //imgURL is the url of the image on the internet
    UIMarkupTextPrintFormatter *htmlPrintF = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:markupText];
    htmlPrintF.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
    htmlPrintF.maximumContentWidth = 6 * 72.0;

Ideally I don't want to be passing in the image URL since it takes up time downloading an image when it's already been downloaded and displayed once, and is retained in a UIImage object. The code above gives me the appropriate margins but the image is not sized appropriately to fit on the page.

It would be easier to just go back to using UIPrintInteractionController and ditch the UIActivityViewController, however I would like to have everything in one place, in UIActivityViewController.

Any ideas on how to control margins / image size when using UIActivityViewController?

like image 264
SMSidat Avatar asked Nov 13 '13 21:11

SMSidat


1 Answers

This might look like a workaround but it will definitely help.

You can use -viewPrintFormatter method of UIImageView if you have the image already downloaded. You will need to create a UIImageView before presenting UIActivityViewController.

You can adjust contentInsets and maximumContentWidth etc. as you proposed.

Below sample takes an image, creates an imageView for resizing purposes and using its printFormatter.

- (void)showActivityWithImage:(UIImage *)image
{
    UIImageView *imageview = [[UIImageView alloc] initWithImage:image];

    CGFloat aspectRatio = image.size.width / image.size.height;
    CGFloat maxWidth = 6.0f * 72.0f;

    // resize image with aspect ratio if bigger than print size
    // you can use a smarter resize here considering maxHeight also

    if (image.size.width > maxWidth)
    {
        CGFloat height = maxWidth / aspectRatio;
        [imageview setFrame:CGRectMake(0.0f, 0.0f, maxWidth, height)];
    }

    UIViewPrintFormatter *printFormatter = [imageview viewPrintFormatter];

    printFormatter.contentInsets = UIEdgeInsetsMake(72.0f, 72.0f, 72.0f, 72.0f);
    printFormatter.maximumContentWidth = maxWidth;

    // printFormatter is added only for print activity
    // image is used by others such as 'save image', 'mail'

    NSArray *activityItems = @[image, printFormatter];


    // create and present activityViewController

    UIActivityViewController *activityController;
    activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems
                                                           applicationActivities:nil];

    [self presentViewController:activityController
                       animated:YES
                     completion:nil];
}
like image 194
ryumer Avatar answered Sep 26 '22 16:09

ryumer