Skip to content

Latest commit

 

History

History
49 lines (37 loc) · 1.05 KB

File metadata and controls

49 lines (37 loc) · 1.05 KB

DetectImageType

Detect image file format from binary data by analyzing magic bytes (file signatures).

Supported Formats

Format Signature
PNG 89 50 4E 47 0D 0A 1A 0A
JPEG FF D8 FF
JPEG2000 00 00 00 0C 6A 50 20 20 or FF 4F FF 51
GIF GIF87a or GIF89a
BMP BM
TIFF II*\0 or MM\0*
ICO 00 00 01 00
ICNS icns
WebP RIFF....WEBP
HEIC/HEIF ftyp box with heic/heix/hevc/mif1 brands
AVIF ftyp box with avif/avis brands
SVG <?xml or <svg prefix

Usage

// From Data
let type = imageData.detectImageType()
print(type)              // .png
print(type.mimeType)     // "image/png"
print(type.fileExtension) // "png"

// From file path
let type = Data.detectImageType(with: "/path/to/image.heic")

// From URL
let type = Data.detectImageType(with: imageURL)

// From bundle resource
if let type = Data.detectImageType(with: "photo.jpg") {
    print(type)
}

Installation

Copy Data+Extension.swift into your project.

License

MIT