swift - 解析视频上传 - fatal error : unexpectedly found nil while unwrapping an Optional Value

标签 swift video parse-platform option-type

我正在尝试将视频上传到 Parse

如果我只是尝试像这样上传视频:

let videoData = NSData(contentsOfURL: url)
let videoFile = PFFile(name: "video.mov", data: videoData)
videoUploadObject["Video"] = videoFile

我会收到错误。我试图通过运行如下所示的内容来删除它。

Main.sharedMain.userVideoOutputURL

返回:

file:///private/var/mobile/Containers/Data/Application/3B78A154-4340-432B-817A-2857EBA8064A/tmp/video.mov

这是我的完整代码:

let url = Main.sharedMain.userVideoOutputURL!

    if let videoData = NSData(contentsOfURL: url) {

        let videoFile = PFFile(name: "video.mov", data: videoData)
        videoUploadObject["Video"] = videoFile


        print("Video File \(videoFile)")


    } else {
        print("Else")
    }

总是打印 Else?我该怎么做才能解决这个问题?谢谢!

最佳答案

URL 看起来不错(iOS 不使用“/User/blah/blah/blah”来存储应用程序)。

我的第一个想法是检查数据是否正确写入。 NSData 有一个“writeToFile: options:”方法返回一个 bool 值。如果写入失败,这将返回 false,并且还会抛出一个错误,该错误应该会为您提供一些额外的信息。

let url = <YOUR_URL>
do {
    // put your options in here
    try data.writeToURL(url, options: NSDataWritingOptions.DataWritingAtomic)
} catch _ {
    print(error.localizedDescription)
}

如果写入成功,那么您将要尝试使用“contentsWithUrl: options:”加载数据。如果失败,这也会抛出一个错误,因此使用抛出的错误对象的“localisedDescription”属性,您应该能够弄清楚为什么它没有加载。

let url = <YOUR_URL>
var data: NSData? = nil
do {
    // put your options in here
    try data = NSData(contentsOfURL: url, options: NSDataReadingOptions.DataReadingUncached)
} catch _ {
    print(error.localizedDescription)
}

希望对您有所帮助:)

关于swift - 解析视频上传 - fatal error : unexpectedly found nil while unwrapping an Optional Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37246238/

相关文章:

Swift Parse 将 MapView 中的注释保存为 PFGeoPoint

ios - UISplitViewController 的详细 View 中缺少标题栏

java - 使用解析时出现问题 - 无法解析对象

android - 应用程序在查询托管解析服务器时崩溃

matlab - 循环帧提取matlab

multithreading - FFmpeg 对 avcodec_send_packet() 做了什么?

video - ffmpeg - transcoding.c - 为什么最后一个数据包被丢弃

Java 代码到 Swift 转换器

swift - 如何使用 AVAudioPlayerNode 显示歌曲的当前时间和持续时间

javascript - 如何在 rollupjs 中禁用 Tree shaking