Compose UIActivityTypeMessage with UIImage
Asked Answered
A

2

12

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.

Armington answered 26/9, 2012 at 1:13 Comment(0)
K
1

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];
}
Kolinsky answered 18/6, 2013 at 19:58 Comment(1)
Confirmed that in iOS7, sharing to iMessage works with the standard UIActivityController. As an additional bonus, you can also share a video via iMessage and AirDrop without first having to extract it yourself. In both cases, pass the NSURL of the ALAsset in the activityItems.Kolinsky
C
2

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]
Counterblast answered 26/9, 2012 at 14:38 Comment(5)
This is exactly what I'm doing. Adding an array to the UIActivityViewController. All works just fine. I can share the image to Mail, Twitter, Facebook, Weibo, Copy, Print, save to camera roll.. etc. But I want to share the image via MMS (iMessage) It does NOT allow composing with a UIImage. ONLY strings and nsurl.Armington
Ahh. I did misread your question. Sorry! And I just checked and you're right of course. Text only for Message...strange.Counterblast
Sadly inside apps sending images with the message view (even with UIActivityViewController) is impossible. My solution for this is copy the image to the pasteboard, instruct the user to paste the image into their message, and then launch the iOS message app using UIApplication...Edam
@PsychoDad, Apple camera app supports MMS feature for camera roll image sharing. Do you have any idea about this?Cleanthes
@CornDogComputers, were you able to add the image when you send MMS(imessage)?Attlee
K
1

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];
}
Kolinsky answered 18/6, 2013 at 19:58 Comment(1)
Confirmed that in iOS7, sharing to iMessage works with the standard UIActivityController. As an additional bonus, you can also share a video via iMessage and AirDrop without first having to extract it yourself. In both cases, pass the NSURL of the ALAsset in the activityItems.Kolinsky

© 2022 - 2024 — McMap. All rights reserved.