首页 > 解决方案 > 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)
        }
    })
}

标签: iosswiftxcodeuiimagepickercontrollerios12

解决方案


您应该在后台队列而不是主队列中执行此操作。并且不要对选项使用强制解包,而是使用guardor安全地解包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)
    }
}

推荐阅读