我已经捕获了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)
    }
}

10-08 05:50