我应该为 UTTypeImage 导入什么库,它是 iOS15 中 kUTTypeImage 的替代品?

What's the library should I import for UTTypeImage, which is the replacement of kUTTypeImage in iOS 15?

在iOS15之前,我使用UIImagePickerController来抓取图片和视频,我从[UIImagePickerController.InfoKey : Any]获取mediaType,然后我使用kUTTypeImage(在MobileCoreServices库中) 来识别媒体类型。

然而,当谈到 iOS 15 时,Xcode 抱怨说 kUTTypeImage was deprecated in iOS 15.0. Use UTTypeImage instead. 所以,我用 UTTypeImage 替换了 kUTTypeImage,但是 Xcode 不知道。

尝试搜索一些信息,但没有得到任何线索。我想我应该导入正确的库,但它是什么?

部分代码如下:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let mediaType = info[.mediaType] as? String else { return }
        switch mediaType {
        case String(kUTTypeImage):
        // blabla
        case String(kUTTypeMovie):
        // blabla

下面是一些截图:

有点混乱。首先,您需要 import UniformTypeIdentifiers。然后,将 kUTTypeImage 替换为 UTType.image (the Swift version of UTTypeImage).

如果您正在寻找 URL 扩展名的替代品 post:

iOS15 UTType deprecations for URL-extension

给你一个更短的版本


import Foundation
import UniformTypeIdentifiers

extension URL {
   
    func mimeType() -> String {
        let pathExtension = self.pathExtension
        if let type = UTType(filenameExtension: pathExtension) {
            if let mimetype = type.preferredMIMEType {
                return mimetype as String
            }
        }
        return "application/octet-stream"
    }
    
    
    func conforms(to type: UTType) -> Bool{
        let uttype = UTType(mimeType: mimeType())
        
        return uttype?.conforms(to: type) ?? false
        
        
    }
}

用法:

url.conforms(to: .json)