【Use】 open func loadDataRepresentation(forTypeIdentifier typeIdentifier: String, completionHandler: @escaping (Data?, Error?) -> Void) -> Progress
if self.hasItemConformingToTypeIdentifier(UTType.webP.identifier) {
return try await self.loadDataRepresentation(forTypeIdentifier: UTType.webP.identifier)
}
At this point you can load webp Image。But it is impossible to judge this time webp through Data, because he is indistinguishable from jpeg。
extension Data { var isWebP: Bool {
// Ensure the size of the data is large enough
// for us to properly check.
guard self.count >= 12 else {
return false
}
return withUnsafeBytes { bytes in
// The first 4 bytes are the ASCII letters "RIFF"
// Skipping 4 bytes for the file size, the next 4 bytes after
// that should read "WEBP"
if String(decoding: bytes[0..<4], as: UTF8.self) != "RIFF" ||
String(decoding: bytes[8..<12], as: UTF8.self) != "WEBP" {
return false
} return true
}
}