iosswiftuiimagepngmfmailcomposer

When attaching PNG to MFMailComposer, images auto-rotate to landscape swift


I am making a simple camera app where a user takes an image and then emails it. I have one problem: once the user takes an image (which always works), if it is portrait, the MFMailComposer auto-rotates it to landscape incorrectly, making everything sideways. How can I stop this behavior?


Solution

  • This occurs because PNGs do not store orientation information. Attach the photo to the email as a JPG instead and it will be oriented correctly!

    Use this code to attach your image instead:

    let data:NSData = UIImageJPEGRepresentation(image, 0.9)! // 0.9 is compression value: 0.0 is most compressed/lowest quality and 1.0 is least compressed/highest quality
    mailcomposer.addAttachmentData(data, mimeType: "image/jpg", fileName: "image.jpg")
    

    Source + more info: https://stackoverflow.com/a/34796890/5700898