13

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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.
I mean to change the extension to jpg here in filename property- [mailComposer addAttachmentData: UIImageJPEGRepresentation(viewImage,1.0f) mimeType:@"" fileName:@"myImage.jpg"];
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.