![](/img/trans.png)
[英]How to get file name and file size from UIDocumentPickerViewController in iOS 14?
[英]Initialization of UIDocumentPickerViewController in iOS 14
我有一个UIDocumentPickerViewController
只选择图像。 这在 iOS 13 中运行良好:
let supportedTypes: [String] = ["public.image"]
let documentPickerViewController = UIDocumentPickerViewController(documentTypes: supportedTypes, in: .import)
我试图在 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)
这甚至无法编译,因为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)
将import UniformTypeIdentifiers
添加到您的 swift 文件以访问UTType
有关更多用法,请参阅文档。
以及WWDC2020 视频“在 SwiftUI 中构建基于文档的应用程序”进行实际演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.