Finding image type from NSData or UIImage
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
The most reliable way to detect an image type is to inspect the original bytes, not the UIImage alone. Once an image has been decoded into a UIImage, the original container format may already be lost, so if you care about whether the source was JPEG, PNG, GIF, or something else, start from Data.
Check Magic Numbers in the Data Header
Most common image formats begin with recognizable signature bytes, often called magic numbers.
Examples include:
- JPEG starts with
FF D8 FF - PNG starts with
89 50 4E 47 - GIF starts with
47 49 46 38
In Swift, you can inspect the first byte or several bytes.
This is fast and usually sufficient for common formats.
UIImage Does Not Guarantee the Original Format
A UIImage represents decoded image content, not necessarily the original file container. If you create a UIImage from data and then inspect only the image object, you may know dimensions and pixels but not whether the source file was JPEG or PNG.
That is the key architectural point: container format lives in the bytes.
If you still have the original Data, keep it available when file-type detection matters.
Use Image I/O for Richer Type Detection
If you want a more system-level approach, Image I/O can inspect the source and return a type identifier.
That can tell you the type in a form suitable for further processing and can be cleaner than hand-maintaining a growing list of signatures.
Re-Encoding Changes the Story
If you convert a UIImage back into data with a JPEG or PNG encoder, you are creating a new container format. That means a UIImage may be rendered from one format and then later saved as another.
So when the requirement is “what was the original image type,” only the original bytes answer that question reliably.
Use the Detection Result for Real Decisions
Format detection often matters because different formats support different features.
Examples:
- PNG supports transparency well
- JPEG is lossy and common for photos
- GIF may imply animation
So the file-type check is often a gateway to another rule such as compression choice, upload validation, or conversion behavior.
Common Pitfalls
- Trying to infer the original file format from
UIImagealone. - Inspecting too few bytes when implementing manual signature checks.
- Assuming every image format can be identified by only one first byte in every case.
- Discarding the original
Dataand later needing container-specific behavior. - Re-encoding an image and forgetting that the output format may differ from the input format.
Summary
- Detect image type from the original bytes whenever possible.
- Magic-number checks are a simple and effective solution for common formats.
- '
UIImagedoes not reliably preserve the original container format.' - Image I/O can provide richer type detection when needed.
- Keep the original
Dataif later code depends on the source format.
Related reading
- Finding index of character in Swift String
- Finding sum of elements in Swift array
- Finding the direction of scrolling in a UIScrollView?
- findViewById in Fragment
- findViewById in Fragment
- findViewByID returns null
- Firebase Crashlytics Upload missing dSYMs to see crashes from 1 versions.iOS
- Firebase for iOS, GoogleService-Info.plist property IS_ANALYTICS_ENABLED set to NO
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.