问题
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.
回答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];
}
回答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: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/6-Default-568h@2x.png
// http://anglerally.com/wp-content/uploads/2012/09/7-Default-568h@2x.png
// iPad:
// http://anglerally.com/wp-content/uploads/2012/09/6-Default-Portrait@2xipad.png
// http://anglerally.com/wp-content/uploads/2012/09/7-Default-Portrait@2xipad.png
来源:https://stackoverflow.com/questions/12593255/compose-uiactivitytypemessage-with-uiimage