7

The image attached is just "Attachment-1", no extension. How do I specify one ?

    NSData *compressedImage = UIImageJPEGRepresentation(self.resultImage, 0.8 );

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[ @"Check this out!", compressedImage ] applicationActivities:nil];

    [self.navigationController presentViewController:activityViewController animated:YES completion:nil];
Thomas Joulin
  • 6,590
  • 9
  • 53
  • 88

2 Answers2

14

According to this answer you should be able to use this workaround to specify a filename

NSData *compressedImage = UIImageJPEGRepresentation(self.resultImage, 0.8 );
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [docsPath stringByAppendingPathComponent:@"image.jpg"];
NSURL *imageUrl     = [NSURL fileURLWithPath:imagePath];

[compressedImage writeToURL:imageUrl atomically:YES]; // save the file
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[ @"Check this out!", imageUrl ] applicationActivities:nil];

The obvious drawback of this approach is that you will have to save the image on disk.

Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • 1
    Thanks, that should work. It's annoying that Apple doesn't take this kind of functionality more seriously. Somebody at Google needs to give them a lesson on sandboxed app interop, Android has been doing this so well since the beginning with Intents. ... *~sigh~* hopefully iOS 7 changes it. – Amir Memon Aug 16 '13 at 22:14
  • This helps for sharing .png files as well! When you just share a UIImage, it is converted into a jpeg automatically. Great solution! – Klaas Jan 20 '14 at 20:01
  • 3
    If you pass the NSData of the image in the activityItems array it also works. You don't need to actually write out to a file then. – Jamie Hamick Feb 10 '15 at 01:24
  • @timeuser but it does not allow you to change the filename, except for having a default image1.jpg ? – Peterdk Aug 14 '17 at 13:33
  • This doesn't seem to work for me. If I pass the file name in the activity items array then it just tries to save 2 files... one is a text document containing the name, and the other is the actual file/data I'm trying to save. – user3344977 Nov 19 '20 at 18:32
0

Credit to timeuser's suggestion, converting UIImage to NSData.

NSData * data = UIImageJPEGRepresentation(original,1.0);
UIActivityViewController * activity =[[UIActivityViewController alloc] initWithActivityItems:@[@"Your Text", data] applicationActivities:nil];
[self presentViewController:activity animated:true completion:^{ }];
Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55