12

I'm sharing an image, along with a url and some text, with a UIActivityViewController. Everything works great, except the image file name is always 'Image-1' with no extension. In some cases this causes a big problem, such as in the mail app which won't open an image without an extension at all.

The image is being generated by the application and exists as a UIImage.

Is there a way to control the file name and add the correct extension to the image?

Anthony Mattox
  • 7,048
  • 6
  • 43
  • 59

2 Answers2

0

If its application generated image (UIImage reference), then you can use below code:

[mailComposer addAttachmentData: UIImagePNGRepresentation(viewImage) mimeType:@"" fileName:@"myImage.png"];

I have used the same code for my project, where one can create your own PNG format image with UIImagePNGRepresentation() method.

If you require JPEG image then use UIImageJPEGRepresentation() method. Then change the filename in above method with .jpg extension.

For more details regarding both the methods, refer UIKit Class Reference

Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • Thanks! I had just tried this and it fixes the issue with the mail app. After creating the jpeg data, how would I go about changing the file name though? `UIActivityViewController` has no analogous way to name an attachment. – Anthony Mattox Mar 18 '13 at 14:47
  • I mean to change the extension to jpg here in filename property- [mailComposer addAttachmentData: UIImageJPEGRepresentation(viewImage,1.0f) mimeType:@"" fileName:@"myImage.jpg"]; – Mrunal Mar 18 '13 at 14:48
  • 10
    Right, however I'm using a `UIActivityViewController` to email the image, so I can't manually attach the image to the mail composer in this way. – Anthony Mattox Mar 18 '13 at 14:55
0

The only way I'm aware to control the filename or the encoding type of a UIImage is to save it to a file on the device, and then feed the URL of the file into the UIActivityViewController. Take a look at this answer for code you can use to accomplish this. This approach has some obvious drawbacks, but will probably be good enough for most applications.

Community
  • 1
  • 1
jzn
  • 656
  • 10
  • 17