Thomas Joulin Thomas Joulin - 4 months ago 30
iOS Question

UIActivityViewController sharing image via email has no extension

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];

Answer

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.