image format
NSData
UIImage
image type detection
Swift programming

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.

Practice algorithms

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.

swift
1import Foundation
2
3enum ImageType {
4    case jpeg, png, gif, unknown
5}
6
7func detectImageType(data: Data) -> ImageType {
8    let bytes = [UInt8](data.prefix(8))
9    guard let first = bytes.first else { return .unknown }
10
11    switch first {
12    case 0xFF:
13        return .jpeg
14    case 0x89:
15        return .png
16    case 0x47:
17        return .gif
18    default:
19        return .unknown
20    }
21}

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.

swift
1import Foundation
2import ImageIO
3
4func imageUTI(from data: Data) -> String? {
5    guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {
6        return nil
7    }
8    return CGImageSourceGetType(source) as String?
9}

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 UIImage alone.
  • 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 Data and 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.
  • 'UIImage does not reliably preserve the original container format.'
  • Image I/O can provide richer type detection when needed.
  • Keep the original Data if later code depends on the source format.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
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.

Practice algorithms