Swift 使用AVAssetExportSession将视频附加到电子邮件
我对编程相当陌生,如果我的代码太离谱,我很抱歉,我正在尝试使用AVAssetExportSession将AVAsset(视频)转换为数据,以便使用MFMailComposer将其附加到电子邮件。我花了很长时间想弄明白这件事已经结束了 我不知道如何访问导出的文件,因此在启动mailComposer之前不知道将数据设置为什么 我接近了吗?任何帮助都将不胜感激!谢谢Swift 使用AVAssetExportSession将视频附加到电子邮件,swift,xcode,avassetexportsession,Swift,Xcode,Avassetexportsession,我对编程相当陌生,如果我的代码太离谱,我很抱歉,我正在尝试使用AVAssetExportSession将AVAsset(视频)转换为数据,以便使用MFMailComposer将其附加到电子邮件。我花了很长时间想弄明白这件事已经结束了 我不知道如何访问导出的文件,因此在启动mailComposer之前不知道将数据设置为什么 我接近了吗?任何帮助都将不胜感激!谢谢 func createVideo() { let uiImages = self.images let settings
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")
}
}
})
}
}
func createVideo(){
让uiImages=self.images
让设置=CXEImagesToVideo.videoSettings(编解码器:AVVideoCodecH264,宽度:(uiImages[0].cgImage?.width)!,高度:(uiImages[0].cgImage?.height)!)
让movieMaker=CXEImagesToVideo(视频设置:设置)
createMovieFrom(images:uiImages){(fileURL:URL)在
var video=AVAsset(url:fileURL)
让exportPath=NSTemporaryDirectory().appendingFormat(“/video.mp4”)
让exportURL=URL(fileURLWithPath:exportPath)
let exporter=AVAssetExportSession(资产:视频,预设名称:AVAssetExportPresetMediumQuality)
导出器!.outputFileType=AVFileTypeMPEG4
导出器?.outputURL=导出器URL
exporter?.exportAsynchronously(completionHandler:{
如果exporter?.status==AVAssetExportSessionStatus.completed{
如果MFMailComposeViewController.canSendMail(){
让mail=MFMailComposeViewController()
mail.mailComposeDelegate=self
var subject=“这是您的GIF”
var body=“这是您的GIF!!”
如果让emailDescription=UserDefaults.standard.object(forKey:“emailDescription”)作为?[String:String]{
如果让主题=电子邮件描述[“主题”]{
主题=主题
}
如果让body=emailDescription[“body”]{
身体=身体
}
}
mail.setSubject(主题)
setMessageBody(body,isHTML:true)
let data=URL(fileURLWithPath:exportURL)
mail.addAttachmentData(数据,mimeType:“video/mp4”,文件名:“gif.mp4”)
}
}
})
}
}
exportAsynchronously不会在完成处理程序中传递任何参数。您需要传递数据,但如果您的电影在exporter?成功导出,则将void传递给addAttachmentData方法。outputURL
您需要从该文件url加载数据,并将该数据传递给addAttachmentData方法@LeoDabus我更改了完成处理程序,修复了其中两个问题。我仍然不知道如何访问导出的视频(数据)。如果let data=try,有没有办法说“获取导出URL的内容?”?数据(内容:解包){