使用AVAssetExportSession将视频附加到电子邮件

时间:2017-04-14 02:04:38

标签: swift xcode avassetexportsession

我对编程很新,我很抱歉,如果我的代码已经关闭,我正在尝试使用AVAssetExportSession将AVAsset(视频)转换为数据,以便我可以使用MFMailComposer将其附加到电子邮件中。我有一段时间试图弄清楚这已经完成了。

我无法弄清楚如何访问导出的文件,所以在启动mailComposer之前我不知道要将数据设置为什么。

我关门了吗?任何帮助都会非常感激!感谢。

func createVideo() {
    let uiImages = self.images
    let settings = CXEImagesToVideo.videoSettings(codec: AVVideoCodecH264, width: (uiImages[0].cgImage?.width)!, height: (uiImages[0].cgImage?.height)!)
    let movieMaker = CXEImagesToVideo(videoSettings: settings)
    movieMaker.createMovieFrom(images: uiImages) { (fileURL:URL) in
        var video = AVAsset(url: fileURL)

        let exportPath = NSTemporaryDirectory().appendingFormat("/video.mp4")
        let exportURL = URL(fileURLWithPath: exportPath)
        let exporter = AVAssetExportSession(asset: video, presetName: AVAssetExportPresetMediumQuality)
        exporter!.outputFileType = AVFileTypeMPEG4
        exporter?.outputURL = exportURL
        exporter?.exportAsynchronously(completionHandler: {
            if exporter?.status == AVAssetExportSessionStatus.completed {

                if MFMailComposeViewController.canSendMail() {
                    let mail = MFMailComposeViewController()
                    mail.mailComposeDelegate = self
                    var subject = "Here's your GIF"
                    var body = "<p>Here is your GIF!!</p>"
                    if let emailDescription = UserDefaults.standard.object(forKey: "emailDescription") as? [String : String] {
                        if let theSubject = emailDescription["subject"] {
                            subject = theSubject
                        }
                        if let theBody = emailDescription["body"] {
                            body = theBody
                        }
                    }
                    mail.setSubject(subject)
                    mail.setMessageBody(body, isHTML: true)

                    let data = URL(fileURLWithPath: exportURL)
                        mail.addAttachmentData(data, mimeType: "video/mp4", fileName: "gif.mp4")
                }
            }

        })


    }
}

0 个答案:

没有答案
相关问题