ios - 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)
}
})
}
解决方案
您应该在后台队列而不是主队列中执行此操作。并且不要对选项使用强制解包,而是使用guard
or安全地解包if 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)
}
}
推荐阅读
- javascript - 问题安装 sqlite3 (..\src\database.cc(35): error C2248: 'Napi::Env::DefaultFini': 无法访问在类 'Napi::Env' 中声明的私有成员
- r - 使用 R VennDiagram 进行颜色填充:“'填充'的意外参数长度”
- python-3.x - 排查 Python api 请求错误消息“远程结束关闭连接没有响应”
- python - pytorch闪电模型的输出预测
- database - 允许具有空值和特殊值的 Oracle 唯一索引
- c# - 如何为 DataGridView 字段指定我自己的比较器代码?
- visual-studio-code - 如何停止具有彩色选项卡名称的 VSCode
- performance - 在 jmeter 中使用流模式进行分块编码和 sun.net.www.protocol.http.HttpURLConnection writeRequests
- python - Numpy 数组作为模型的输入
- python - Python:OSError:无法打开共享对象文件:Ubuntu 20.04 上没有这样的文件或目录