视频冻结 iphone 6 swift 4.2

Video freezing in iphone 6 swift 4.2

我已经拍摄了 5 秒的视频,视频应该以加密格式保存在应用程序的本地存储中。所以我正在使用 RNEncryptor 框架来加密视频。但有时当我点击使用视频按钮应用程序时会冻结。检查下面我的代码是否加密。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    DispatchQueue.main.async(execute: {
        let encryptData = try? RNCryptor.encrypt(data: data!, withPassword: "ABC123")
        do {
            try encryptData?.write(to: url!, options:.withoutOverwriting)
            self.encryptVideoData = encryptData as! NSData
            UserDefaults.standard.set(self.encryptVideoData, forKey: "passportVidKey")
        } catch { // handle error
            print(error)
        }
    })
}

您应该在后台队列而不是主队列中执行此操作。并且不要对可选项使用强制解包,而是使用 guardif let 安全地解包。声明。下面的例子可以提供帮助,

DispatchQueue.global(qos: .background).async {
    guard
        let data = data,
        let url = url,
        let encryptData = try? RNCryptor.encrypt(data: data, withPassword: "ABC123")
        else { return }
    do {
        try encryptData?.write(to: url, options:.withoutOverwriting)
        self.encryptVideoData = encryptData as! NSData
        UserDefaults.standard.set(self.encryptVideoData, forKey: "passportVidKey")
    } catch { // handle error
        print(error)
    }
}