BX69
BX69

Reputation: 431

Take a screenshot of app then attach in Message using Swift

I am trying to take a screenshot of my app then send it to a contact in a message. The screenshot is taken just fine when I view it in my Photo Library... but when the message composer opens the image it appears with "?" like the mime type isn't correct. I am converting the image to NSData with UIImageJPEGRepresentation. What am I doing wrong? Many thanks

func screenShotMethod() {


    if (messageComposer.canSendText()) {
        // Obtain a configured MFMessageComposeViewController
         //Create the UIImage
        UIGraphicsBeginImageContext(view.frame.size)

        view.layer.renderInContext(UIGraphicsGetCurrentContext())
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        //Save it to the camera roll
        //UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

        let messageComposeVC = messageComposer.configuredMessageComposeViewController()
        //messageComposeVC.body = image
        var imageData = UIImageJPEGRepresentation(image, 1.0)
        messageComposeVC.addAttachmentData(imageData, typeIdentifier: "image/jpeg", filename: "My Image")
        presentViewController(messageComposeVC, animated: true, completion: nil)
    }
    else
    {
        println("No good")
    }
}

Upvotes: 3

Views: 1507

Answers (2)

Jayesh Miruliya
Jayesh Miruliya

Reputation: 3317

controller.addAttachmentData(UIImageJPEGRepresentation(screenShot, CGFloat(1.0))!, typeIdentifier: "image/jpeg", filename: "test.jpg")

Upvotes: 0

BX69
BX69

Reputation: 431

I figured out the issue myself. The filename needs an extension. I added ".jpeg" like so:

    messageComposeVC.addAttachmentData(imageData, typeIdentifier: "image/jpeg", filename: "My Image.jpeg")

Upvotes: 5

Related Questions