0

I am using iOS 6's UIActivityViewController. I would like to share an image that is not available locally on my iPhone, but that it is available on a remote URL.

NSString *textToShare = _eventoTitle;
UIImage *imageToShare = [UIImage imageNamed:_iconUrl];
NSURL *url = [NSURL URLWithString:_permalink];
NSArray *activityItems = [[NSArray alloc]  initWithObjects:textToShare, imageToShare, url, nil];

Unfortunately, this is not working. What am I doing wrong?

I have also tried to use the AFNetworking library:

UIImage *imageToShare = [UIImage alloc];
[*imageToShare setImageWithURL:[NSURL URLWithString:_iconUrl]];

This is not working too.

_iconUrl is something like http://www.mysite.com/picture.png

Thank you, Francesco

2 Answers2

4

Try with:

UIImage *imageToShare = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", _iconUrl]]]];

Matteo

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • 6
    FYI this is a synchronous network operation, so if this is done on the main thread your app will hang until the image is downloaded. Bad bad bad. – Kenny Winker Feb 06 '13 at 22:23
  • 1
    You may want to look at using a subclass of `UIActivityItemProvider` to provide a placeholder that is later realized. – jessecurry Jul 31 '13 at 18:51
4

To use remote images, I implemented a UIActivityItemProvider subclass that downloads the image when requested by the UIActivityViewController. UIActivityViewController calls your UIActivityItemProvider on a background thread so at least it doesn't block the main UI. I use a synchronous call just like Matteo suggests inside my UIActivityItemProvider. However, it's really still not a great solution because it just delays when you have to go do the expensive download. UIActivityViewController doesn't request your data until the user picks one of the activity icons in the view controller. At that time, it calls the UIActivityItemProvider to get the data. So you get a delay at this time.

Mark Krenek
  • 4,889
  • 3
  • 25
  • 17