Use UniformTypeIdentifiers framework for everything

This commit is contained in:
Shadowfacts 2021-06-07 20:08:46 -04:00
parent ef1db466b9
commit d667f6362c
2 changed files with 10 additions and 10 deletions

View File

@ -8,7 +8,7 @@
import Foundation import Foundation
import UIKit import UIKit
import MobileCoreServices import UniformTypeIdentifiers
final class CompositionAttachment: NSObject, Codable, ObservableObject { final class CompositionAttachment: NSObject, Codable, ObservableObject {
static let typeIdentifier = "space.vaccor.Tusker.composition-attachment" static let typeIdentifier = "space.vaccor.Tusker.composition-attachment"
@ -52,10 +52,10 @@ final class CompositionAttachment: NSObject, Codable, ObservableObject {
extension CompositionAttachment: Identifiable {} extension CompositionAttachment: Identifiable {}
private let imageType = kUTTypeImage as String private let imageType = UTType.image.identifier
private let mp4Type = kUTTypeMPEG4 as String private let mp4Type = UTType.mpeg4Movie.identifier
private let quickTimeType = kUTTypeQuickTimeMovie as String private let quickTimeType = UTType.quickTimeMovie.identifier
private let dataType = kUTTypeData as String private let dataType = UTType.data.identifier
extension CompositionAttachment: NSItemProviderWriting { extension CompositionAttachment: NSItemProviderWriting {
static var writableTypeIdentifiersForItemProvider: [String] { static var writableTypeIdentifiersForItemProvider: [String] {
@ -100,11 +100,11 @@ extension CompositionAttachment: NSItemProviderReading {
return try PropertyListDecoder().decode(Self.self, from: data) return try PropertyListDecoder().decode(Self.self, from: data)
} else if UIImage.readableTypeIdentifiersForItemProvider.contains(typeIdentifier), let image = try? UIImage.object(withItemProviderData: data, typeIdentifier: typeIdentifier) { } else if UIImage.readableTypeIdentifiersForItemProvider.contains(typeIdentifier), let image = try? UIImage.object(withItemProviderData: data, typeIdentifier: typeIdentifier) {
return CompositionAttachment(data: .image(image)) as! Self return CompositionAttachment(data: .image(image)) as! Self
} else if typeIdentifier == mp4Type || typeIdentifier == quickTimeType { } else if let type = UTType(typeIdentifier), type == .mpeg4Movie || type == .quickTimeMovie {
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let temporaryFileName = ProcessInfo().globallyUniqueString let temporaryFileName = ProcessInfo().globallyUniqueString
let fileExt = UTTypeCopyPreferredTagWithClass(typeIdentifier as CFString, kUTTagClassFilenameExtension)! let fileExt = type.preferredFilenameExtension!
let temporaryFileURL = temporaryDirectoryURL.appendingPathComponent(temporaryFileName).appendingPathExtension(fileExt.takeUnretainedValue() as String) let temporaryFileURL = temporaryDirectoryURL.appendingPathComponent(temporaryFileName).appendingPathExtension(fileExt)
try data.write(to: temporaryFileURL) try data.write(to: temporaryFileURL)
return CompositionAttachment(data: .video(temporaryFileURL)) as! Self return CompositionAttachment(data: .video(temporaryFileURL)) as! Self
} else if NSURL.readableTypeIdentifiersForItemProvider.contains(typeIdentifier), let url = try? NSURL.object(withItemProviderData: data, typeIdentifier: typeIdentifier) as URL { } else if NSURL.readableTypeIdentifiersForItemProvider.contains(typeIdentifier), let url = try? NSURL.object(withItemProviderData: data, typeIdentifier: typeIdentifier) as URL {

View File

@ -8,7 +8,7 @@
import UIKit import UIKit
import Photos import Photos
import MobileCoreServices import UniformTypeIdentifiers
import PencilKit import PencilKit
enum CompositionAttachmentData { enum CompositionAttachmentData {
@ -74,7 +74,7 @@ enum CompositionAttachmentData {
data = context.jpegRepresentation(of: image, colorSpace: colorSpace, options: [:])! data = context.jpegRepresentation(of: image, colorSpace: colorSpace, options: [:])!
mimeType = "image/jpeg" mimeType = "image/jpeg"
} else { } else {
mimeType = UTTypeCopyPreferredTagWithClass(dataUTI as CFString, kUTTagClassMIMEType)!.takeRetainedValue() as String mimeType = UTType(dataUTI)!.preferredMIMEType!
} }
completion(data, mimeType) completion(data, mimeType)