Skip to content
This repository has been archived by the owner on Oct 7, 2019. It is now read-only.

malcommac/ImageSizeFetcher

Repository files navigation

ImageSizeFetcher

Finds the size or type of an image given its uri by fetching as little as needed

Version License Platform CocoaPods Compatible Carthage Compatible Twitter

★★ Star me to follow the project! ★★
Created by Daniele Margutti - danielemargutti.com

What's ImageSizeFetcher

Your app needs to find the size or type of an image but it's not locally stored – it’s on another asset server, or in the cloud (Amazon S3 for example); your webservice does not expose the size via APIs.

You don’t want to download the entire image to your app server – it could be many tens of kilobytes, or even megabytes just to get this information.

Moreover you can't wait the download of all images to adjust your layout (ie. UICollectionView/UITableView) and you can't do it incrementally without incur in a poor UX experience for your user.

For most common image types (GIF, PNG, BMP etc.), the size of the image is simply stored at the start of the file. For JPEG files it’s a little bit more complex, but even so you do not need to fetch much of the image to find the size.

ImageSizeFetcher does this minimal fetch just downloade few kb and doesn’t rely on installing external libraries; it's just in pure Swift.

Supported Formats

Currently ImageSizeFetcher supports the most common formats you can use in your app:

  • PNG
  • GIF
  • JPEG
  • BMP

In most cases the the downloaded data is below 50 KB.

How it works

If you are interested in knowing more about how it works I wrote an article you can found on my blog:

"Prefetching images size without downloading them [entirely] in Swift"

To use it you just need to instantiate (and keep a strong reference) to the ImageSizeFetcher class. It supports a local in-memory cache of the request and has a GCD queue to manage multiple request automatically; you just need to call the sizeFor() method:

let imageURL: URL = ...
fetcher.sizeFor(atURL: $0.url) { (err, result) in
  // error check...
  print("Image size is \(NSStringFromCGSize(result.size))")
}

Unit Tests

Unit tests are available inside the Tests folder.

Requirements

ImageSizeFetcher is compatible with Swift 4.x.

  • iOS 8.0+
  • macOS 10.9+
  • tvOS 9.0+
  • watchOS 2.0+

Issues & Contributions

Please open an issue here on GitHub if you have a problem, suggestion, or other comment. Pull requests are welcome and encouraged.

Installation

Install via CocoaPods

CocoaPods is a dependency manager which automates and simplifies the process of using 3rd-party libraries like ImageSizeFetcher in your projects. You can install it with the following command:

$ sudo gem install cocoapods

CocoaPods 1.0.1+ is required to build ImageSizeFetcher.

Install via Podfile

To integrate ImageSizeFetcher into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

target 'TargetName' do
use_frameworks!
pod 'ImageSizeFetcher'
end

Then, run the following command:

$ pod install

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate ImageSizeFetcher into your Xcode project using Carthage, specify it in your Cartfile:

github "malcommac/ImageSizeFetcher"

Run carthage to build the framework and drag the built ImageSizeFetcher.framework into your Xcode project.

Copyright

ImageSizeFetcher is available under the MIT license. See the LICENSE file for more info.

Daniele Margutti: hello@danielemargutti.com

Twitter: @danielemargutti