Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
拍摄应用程序的屏幕截图,然后使用Swift将其附加到消息中_Swift_Screenshot_Mfmessagecomposeview - Fatal编程技术网

拍摄应用程序的屏幕截图,然后使用Swift将其附加到消息中

拍摄应用程序的屏幕截图,然后使用Swift将其附加到消息中,swift,screenshot,mfmessagecomposeview,Swift,Screenshot,Mfmessagecomposeview,我正在尝试拍摄我的应用程序的屏幕截图,然后将其发送给一个联系人。当我在我的照片库中查看它时,截图拍摄得很好。。。但是当消息生成器打开图像时,它会显示“?”,就像mime类型不正确一样。我正在使用UIImageJPEG表示将图像转换为NSData。我做错了什么?非常感谢 func screenShotMethod() { if (messageComposer.canSendText()) { // Obtain a configured MFMessageCompos

我正在尝试拍摄我的应用程序的屏幕截图,然后将其发送给一个联系人。当我在我的照片库中查看它时,截图拍摄得很好。。。但是当消息生成器打开图像时,它会显示“?”,就像mime类型不正确一样。我正在使用UIImageJPEG表示将图像转换为NSData。我做错了什么?非常感谢

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")
    }
}

我自己解决了这个问题。文件名需要扩展名。我像这样添加了“.jpeg”:


也许你可以解释你的代码,解释为什么它比一年前发布的已有答案更好。。。
    messageComposeVC.addAttachmentData(imageData, typeIdentifier: "image/jpeg", filename: "My Image.jpeg")
controller.addAttachmentData(UIImageJPEGRepresentation(screenShot, CGFloat(1.0))!, typeIdentifier: "image/jpeg", filename: "test.jpg")