ImageScout is a Swift implementation of fastimage. It allows you to find the size and type of a remote image by downloading as little as possible.
Sometimes you need to know the size of a remote image before downloading it, such as
using a custom layout in a UICollectionView
.
ImageScout parses the image data as it is downloaded. As soon as it finds out the size and type of image, it stops the download. The downloaded data is below 60 KB in most cases.
Add the following to your Package.switft
file:
let package = Package(
dependencies: [
.package(url: "https://github.com/kaishin/ImageScout.git", from: "2.1.0")
],
)
github "kaishin/ImageScout"
carthage update
pod 'ImageScout'
use_frameworks!
pod install
with CocoaPods 1.0 or newer.The only method you will be using is scoutImageWithURI()
, with the following full signature:
func scoutImageWithURI(URI: String, completion: (NSError?, CGSize, ScoutedImageType) -> ())
Here's an example:
let scout = ImageScout()
scout.scoutImageWithURI("http://.../image-scout-logo.png") { error, size, type in
if let error = error {
print(error.code)
} else {
print("Size: \(size)")
print("Type: \(type.rawValue)")
}
}
If the image is not successfully parsed, the size is going to be CGSizeZero
and the type .Unsupported
. The error will contain more info about the reason:
⚠️ It's important to keep a strong reference to the ImageScout
instance until the callback completes. If reference is lost, your completion handler will never be executed.
See LICENSE.