尝试在UIImagePickerController didFinishPickingMediaWithInfo回调中移动文件失败,并显示“您没有权限”错误

问题描述

这是我显示图像选择器的代码

        let pickerController = UIImagePickerController()
        pickerController.delegate = self
        pickerController.allowsEditing = false
        pickerController.mediaTypes = [kUTTypeMovie as String]
        pickerController.sourceType = .photoLibrary

在代表中,我有

func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) {
    guard let videoURL = info[.mediaURL] as? URL
        else { return }
    do {
        let newFileLocation = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString + ".mov")
        FileManager.default.moveItem(at: videoURL,to: newFileLocation)
    } catch {
        // “trim.92501481-FA5B-490C-8F55-575DE076C8A1.MOV” Couldn’t be moved because you don’t have permission to access “tmp”
    }
}

我回过头来,在iOS 13.5上尝试了此方法,它工作正常,但是在iOS 13.7中,我遇到了这个奇怪的错误

解决方法

一个简单的解决方法,请更改:

FileManager.default.moveItem(at: videoURL,to: newFileLocation)

FileManager.default.copyItem(at: videoURL,to: newFileLocation)