Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers

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

share|improve this answer
 
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 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 at 14:48
 
Hope JPEG will also work for you. –  Mrunal Mar 18 at 14:49
5  
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 at 14:55

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.