There are several commonly used picture formats.
- PNG
- JPEG
- GIF
- WebP is a Google-made image format optimized for fast delivery over the Web
- TIFF/TIF has been widely used in digital impact, remote sensing, medicine and other fields. TIFF files have a.tif or.tiff suffix
- HEIC iOS11 after apple take pictures of the default format
- HEIF is used to store moving images
So, how to judge. Image format usually stored images of Hex Signature in the Signature (hexadecimal) believe that address can refer to: www.garykessler.net/library/fil…
The first two bytes of JPGE binary data are
Hex Signature
FF D8
Copy the code
PNG
Hex Signature
89 50 4E 47 0D 0A 1A 0A
Copy the code
GIF
Hex Signature
47 49 46 38 37 61 or
47 49 46 38 39 61
Copy the code
TIFF
Hex Signature
49 20 49 or
49 49 2A 00 or
4D 4D 00 2B or
4D 4D 00 2A
Copy the code
HEIC
Hex Signature
00
Copy the code
HEIF
Hex Signature
00
Copy the code
A lot of people on the Internet are copied, they know what the code looks like. But I don’t know why it is necessary to determine information such as HEIC, HEIX, mIF1, etc. It was later found here. file-extension.net/seeker/
WEBP
Hex Signature
52
Copy the code
Why is Webp truncated from 0 to 12? What is the basis of the conversion to ASCII?
You can find it in Google’s official introduction. The header file size is 12Bytes
ASCII
RIFF
WEBP
Developers.google.com/speed/webp/…
Once you understand the principle, it’s code!
enum ImageFormat {
case Unknow
case JPEG
case PNG
case GIF
case TIFF
case WebP
case HEIC
case HEIF
}
extension Data {
func getImageFormat() -> ImageFormat {
var buffer = [UInt8](repeating: 0, count: 1)
self.copyBytes(to: &buffer, count: 1)
switch buffer {
case [0xFF]: return .JPEG
case [0x89]: return .PNG
case [0x47]: return .GIF
case [0x49],[0x4D]: return .TIFF
case [0x52] where self.count >= 12:
if let str = String(data: self[0...11], encoding: .ascii), str.hasPrefix("RIFF"), str.hasSuffix("WEBP") {
return .WebP
}
case [0x00] where self.count >= 12:
if let str = String(data: self[8...11], encoding: .ascii) {
let HEICBitMaps = Set(["heic", "heis", "heix", "hevc", "hevx"])
if HEICBitMaps.contains(str) {
return .HEIC
}
let HEIFBitMaps = Set(["mif1", "msf1"])
if HEIFBitMaps.contains(str) {
return .HEIF
}
}
default: break;
}
return .Unknow
}
}
Copy the code