我正在 Swift 中开发 UIDocumentPickerViewController() 。打开文档选择器后,文件和图像看起来很模糊,我无法选择其中任何一个。我已附上屏幕截图和下面的代码。知道为什么这些文件和图像不可选吗?
extension BecomeMuftiTwoVC: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let selectedFilesURL = urls.first else {
return
}
let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let sandboxFileURL = dir.appendingPathComponent(selectedFilesURL.lastPathComponent)
if FileManager.default.fileExists(atPath: sandboxFileURL.path) {
print("Already File Exist")
}
else {
do{
try FileManager.default.copyItem(at: selectedFilesURL, to: sandboxFileURL)
print("Copied Files")
}
catch{
print("Files Are not exist ")
}
}
}
}
//MARK: - Import File Btn Action
@IBAction func importFile(_ sender: Any) {
let documentPicker = UIDocumentPickerViewController(documentTypes: [KUTTypedPlainText], in: .import)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
present(documentPicker, animated: true, completion: nil)
你的线路
let documentPicker = UIDocumentPickerViewController(documentTypes: [KUTTypedPlainText], in: .import)
似乎只为纯文本文件初始化文档选择器。
试试这个:
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeImage as String], in: .import)
另外,请小心,因为自 iOS14.0 以来,初始化程序
init(documentTypes:in:)
已被弃用。所以对于iOS14及以上版本,你应该使用:
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.heic, .heif, .livePhoto, .image])