Skip to content

Instantly share code, notes, and snippets.

@daehn
Created January 24, 2018 09:50
Show Gist options
  • Save daehn/b8849171c34ed964cca99c5d2a1205d4 to your computer and use it in GitHub Desktop.
Save daehn/b8849171c34ed964cca99c5d2a1205d4 to your computer and use it in GitHub Desktop.
import Foundation
import MobileCoreServices
struct UTType: CustomStringConvertible {
let value: CFString
init?(mimeType: String) {
guard let UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil)?.takeUnretainedValue() else { return nil }
value = UTI
}
var description: String {
return value as String
}
}
extension UTType {
var isImage: Bool {
return UTTypeConformsTo(value, kUTTypeImage)
}
var isSVG: Bool {
return UTTypeConformsTo(value, kUTTypeScalableVectorGraphics)
}
}
let image = UTType(mimeType: "image/jpg")
image?.isImage // true
image?.isSVG // false
let svg = UTType(mimeType: "image/svg+xml")
svg?.isImage // true
svg?.isSVG // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment