bac*_*h-f 20 uipickerviewcontroller ios swift ios14
我有一个UIDocumentPickerViewController只选择图像。这在 iOS 13 中运行良好:
let supportedTypes: [String] = ["public.image"]
let documentPickerViewController = UIDocumentPickerViewController(documentTypes: supportedTypes, in: .import)
Run Code Online (Sandbox Code Playgroud)
我正在尝试在 iOS 14 中实现相同的目标。
但是,init(documentTypes:in:)在 iOS 14.0 中已弃用。
所以我假设我必须使用这个初始化程序: init(forOpeningContentTypes:asCopy:)
该forOpeningContentTypes参数需要一个UTType.
我不确定如何使用/导入它。
像[UTType.Image]吗?
let supportedTypes: [UTType] = [UTType.Image]
self.viewController = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true)
Run Code Online (Sandbox Code Playgroud)
这甚至不会编译,因为Cannot find type 'UTType' in scope.
我也试过import MobileCoreServices甚至import CoreServices,但这没有帮助。
文档初始化为新的文件选择器指向此UTTypeReference -所以我试图用,但它也不能被发现。
我如何才能UIDocumentPickerViewController在 iOS 14 中只选择图像?
更新
这有效(感谢@Warren):
import UniformTypeIdentifiers
let supportedTypes: [UTType] = [UTType.image]
let pickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true)
Run Code Online (Sandbox Code Playgroud)
War*_*ton 26
添加 import UniformTypeIdentifiers到您的 swift 文件以访问UTType
有关更多用法,请参阅文档。
以及用于实际演示的WWDC2020 视频“在SwiftUI 中构建基于文档的应用程序”。
| 归档时间: |
|
| 查看次数: |
4770 次 |
| 最近记录: |