Search code examples
iosswiftxcodeuiimagepickercontrollerios12

Video freezing in iphone 6 swift 4.2


I have captured the video for 5 seconds and the video should be in an encrypted format in the local storage of an app. So that I am using RNEncryptor framework for encrypting the video. But sometimes when I click use video button app get a freeze. check my code below for encrypt.

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)
        }
    })
}

Solution

  • You should be performing this action inside the background queue instead of main queue. And don't use force unwrap for optionals instead safely unwrap with guard or if let. statements. Below example can help,

    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)
        }
    }