将本地url转换为数据类型并上传到服务器swift



如何捕获previewURL数据以便将其上传到服务器?

也就是说,我的目标是获取本地文件url
@Published var previewURL: URL?
// file://123-123-123.mov

然后将其转换为Data类型:

@Published var movieData = Data(count: 0)

,然后上传到我的AWS服务器

func saveVideo() async {

let uploadUrl = URL(string: postUploadData.url!)!
var requestToUpload = URLRequest(url: uploadUrl)
requestToUpload.httpMethod = "PUT"
requestToUpload.httpBody = self.movieData
do {
let (data, _) = try! await URLSession.shared.data(for: requestToUpload)

} catch {
debugPrint("Error loading (url): (String(describing: error))")
}

}

下面是我录制完视频后的回调:

func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
if let error = error {
print("FILE OUTPUT ERROR ----->", error.localizedDescription)
return
}

// CREATED SUCCESSFULLY

self.recordedURLs.append(outputFileURL)

self.previewURL = outputFileURL

}

fileOutput函数中添加以下代码:

let data = try Data(contentsOf: self.previewURL!)
print("DATA FOR MOVIE FILE ----->", data)    
} catch {
print(error)
}

相关内容

  • 没有找到相关文章

最新更新