Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compose UIActivityTypeMessage with UIImage

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.

like image 354
corndogcomputers Avatar asked Sep 26 '12 01:09

corndogcomputers


2 Answers

While it seems possible to get the message app to appear in the share sheet using UIActivityViewController in iOS6, there is some secret to get it to work--iOS7 is a different story, passing the NSURL to the asset works out of the box.

Not sure you are getting your images from the ALAssetsLibrary, but if so, grab the ALAsset using its NSURL rather than pulling the UIImage. The share sheet appears in a fraction of the time. This isn't exactly the code I'm using, but similar. But on iOS6, the activity view controller will not show the messages app so long as there is an image attached.

- (void) presentActivityViewController:(ALAsset*) asset {
    // add this to your viewController class

    NSDictionary *urls = [asset valueForProperty:ALAssetPropertyURLs];
    NSURL *url;
    if ([urls count]) {
        for (NSString *key in urls) {
            // I'm making an assumption that the URL I want is the first URL in the dictionary
            url = [urls objectForKey:key];
            break;
        }
    }

    NSArray *activityItems = @[url];
    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityController animated:YES completion:nil];
}
like image 108
Dan Loughney Avatar answered Nov 13 '22 19:11

Dan Loughney


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: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]
like image 21
Scott Gardner Avatar answered Nov 13 '22 18:11

Scott Gardner