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