Skip to content

zambelz48/ZIMGLoader

Repository files navigation

Image Loader Library for iOS

Build Status Platform Languages Carthage compatible

About

A very simple iOS image loader library written in Swift with in-memory cache support using NSCache.

Installation

Create a file named Cartfile inside your root project, and add this to the Cartfile :

github "zambelz48/ZIMGLoader" "v1.0.1"

Open terminal and execute this :

carthage update --platform iOS

Usages

Simple

let option = ImageRequestOption(
	urlString: "https://www.image-url.com",
	placeholderImage: nil,
	loadingIndicator: nil
)

imageView.loadImage(with: option)

Add placeholder image and indicator view

let placeholderImage = UIImage(named: "your_placeholder_image")

let indicatorView = UIActivityIndicatorView(style: .whiteLarge)
indicatorView.startAnimating()

let option = ImageRequestOption(
	urlString: "https://www.image-url.com",
	placeholderImage: placeholderImage,
	loadingIndicator: indicatorView
)

imageView.loadImage(with: option)

Finished callback

...

imageView.loadImage(with: option) {
	print("Finish load image")
}

Options

let option = ImageRequestOption(
	urlString: "", // Image url (Mandatory)
	placeholderImage: UIImage?, // Placeholder image (Optional)
	loadingIndicator: UIView?, // Indicator view when loading an image (Optional)
	contentMode: UIView.ContentMode, // Content mode (default value is : `.scaleToFill`)
	cached: Bool // Flag whether the image should store in cache (default value is : `true`)
)

Cache support

This library is used NSCache, which is only support in-memory cache strategy. And by default every successful download operation the image will be stored in cache. If you don't want to store in cache, set cached flag in ImageRequestOption into false.

Set maximum capacity of the cache

let desiredMaxCapacity = 200 // in MB
ImageCacheHandler.shared.setCache(maxCapacity: desiredMaxCapacity)

Clearing caches

ImageCacheHandler.shared.clearCachedImages()