Prefetching images size without downloading them [entirely] in Swift

A journey into popular image formats to find how to get their size by fetching as little as needed

Daniele Margutti
Sep 9, 2018 · 7 min read

PNG

An example of PNG file in an HEX editor. The red section is the IHDR chunk with the width and height field highlighted.

GIF

An example of GIF file with the initial width/height fields just after the GIF signature.

JPEG

JPEG File is a bit more complex; we need to find the SFOn frame which is placed without a fixed order. So we should iterate over the date until we found it.

Our Project

ImageFetcher

ImageFetcherOperation

ImageParser

Conclusion

Bonus Track

iOS App Development

Stories and technical tips about building apps for iOS, Apple Watch, and iPad/iPhone

Daniele Margutti

Written by

Senior Developer — Mobile Tech Lead @ Immobiliare.it— www.danielemargutti.com

iOS App Development

Stories and technical tips about building apps for iOS, Apple Watch, and iPad/iPhone