1

我已经集成,UIDocumentPickerDelegate或者UIDocumentMenuDelegate我在从 URL 或 DocumentPicker 获取文件时遇到问题。如何在 iOS swift 中解决这个问题?

4

2 回答 2

5

您可以轻松获取文件,例如,

如果您需要获取图像

    @IBAction func btn_Handler_iCloud(_ sender: Any) {
        let doc = UIDocumentMenuViewController(documentTypes: [String(kUTTypeImage)], in: .import)
        doc.delegate = self
        doc.modalPresentationStyle = .formSheet
        self.present(doc, animated: true, completion: nil)
    }

    func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        documentPicker.delegate = self
        present(documentPicker, animated: true, completion: nil)
    }

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        let data = try! Data(contentsOf: urls[0])
        imageview_Buaty.image = UIImage.init(data: data)
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        dismiss(animated: true, completion: nil)
    }
于 2018-01-29T06:43:02.327 回答
1

从 UIDocumentPickerViewController 获取文档 URL 或数据:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { 
    // Available from iOS 8.0 to iOS 11.0
    self.handleFileSelection(inUrl: url)// Here url is document's URL
}

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    // Available from iOS 11.0
    self.handleFileSelection(inUrl: urls.first!)  // Here urls is array of URLs
}

private func handleFileSelection(inUrl:URL) -> Void {
    do { 
     // inUrl is the document's URL
        let data = try Data(contentsOf: inUrl) // Getting file data here
    } catch {
        dLog(message: "document loading error")
    }
}
于 2018-08-09T10:21:07.223 回答