Compose UIActivityTypeMessage with UIImage

corndogcomputers picture corndogcomputers · Sep 26, 2012 · Viewed 11.3k times · Source

Was wandering if anyone can offer some insight. For the life of me I can't figure out how to send a UIImage with UIActivityTypeMessage all though some say it is possible.

The docs say: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIActivity_Class/Reference/Reference.html

UIActivityTypeMessage

The object posts the provided content to the Messages app. When using this service, you can provide NSString and NSAttributedString objects as data for the activity items. You may also specify NSURL objects whose contents use the sms scheme. Available in iOS 6.0 and later. Declared in UIActivity.h.

So to my understanding I can only send NSString/NSURL. I don't see you it is possible.

I'm using this:

UIImage *image; // some image here.

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[ image ] applicationActivities:nil];

Any help would be much appreciated.

Answer

Scott Gardner picture Scott Gardner · Sep 26, 2012

Here's how to use UIActivityViewController universally, with text and an image.

NSMutableArray *items = [NSMutableArray new];
[items addObject:@"Hello world!"];
[items addObject:[UIImage imageNamed:@"MyImage"]];
NSArray *activityItems = [NSArray arrayWithArray:items];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    [self presentViewController:self.activityViewController animated:YES completion:nil];
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    UIPopoverController *aPopoverController = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
    [aPopoverController presentPopoverFromBarButtonItem:self.actionButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

// Examples:
// iPhone:
// http://anglerally.com/wp-content/uploads/2012/09/[email protected]
// http://anglerally.com/wp-content/uploads/2012/09/[email protected]
// iPad:
// http://anglerally.com/wp-content/uploads/2012/09/[email protected]
// http://anglerally.com/wp-content/uploads/2012/09/[email protected]