获取应用程序的屏幕截图,然后使用Swift在Message中附加

时间:2015-01-23 03:18:05

标签: swift screenshot mfmessagecomposeview

我正在尝试截取我的应用的截图,然后将其发送给消息中的联系人。当我在我的照片库中查看屏幕截图时,截图很正常...但是当消息编辑器打开图像时,它会显示为"?"像哑剧类型是不正确的。我正在使用UIImageJPEGRepresentation将图像转换为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")
    }
}

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:0)

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