Home

Awesome

<p align="center"> <img src="LazyImageLogo.png" title="LazyImage" float=left> </p>

Version License Platform Build Status

Simple and efficient image lazy loading functionality for the iOS written in Swift.

LazyImage offers ease of use and complete control over your images by integrating a very light, need-to-have only, code.

Features

Complete control over your image data

instead of force downloading them continuously.

Installation - Cocoapods

pod 'LazyImage'

Usage

LazyImageView

The simplest way to show an image on an image view is by setting the type to LazyImageView and setting the imageURL property.

The downloaded image will be cached to your image view size for best performance.

Example:


@IBOutlet weak var imageView: LazyImageView!

//Normal image

imageview.image = UIImage(named:"someAsset")

//Network image

imageView.imageURL = "https://domain.com/thepathtotheimage.png"

//Option to cancel the request

let canceled = imageView.cancelRequest()

In case you want to know if the image fails to be retrieved.

Use the LazyImageViewDelegate.


@IBOutlet weak var imageView: LazyImageView!

imageView.delegate = <your delegate target with conforms to LazyImageViewDelegate>

//Set the image URL

imageView.imageURL = "https://domain.com/thepathtotheimage.png"


//If an error occurs this will be called

//url is the image URL failed to be fetched.

func errorDownloadingImage(url:String) -> Void {
}

LazyImage

For complete control you can use the LazyImage object on any UIImageview.

Below are some exampes of loading images

Create a lazy image object that will hold the instance.

It is best that you create one instance per object responsible for the image


lazy var lazyImage:LazyImage = LazyImage()

Without completion closure


self.lazyImage.show(imageView:self.imageView, url:"http://something.com/someimage.png")

With spinner


self.lazyImage.showWithSpinner(imageView:self.imageView, url:"http://something.com/someimage.png")

With completion closure


self.lazyImage.show(imageView:self.imageView, url:"http://something.com/someimage.png") {

(error:LazyImageError?) in

//Image loaded. Do something..
}

self.lazyImage.showWithSpinner(imageView:self.imageView, url:"http://something.com/someimage.png") {

(error:LazyImageError?) in

//Image loaded. Do something..
}

With completion


self.lazyImage.show(imageView:self.imageView, url:"http://something.com/someimage.png", defaultImage:"someLocalImageName") {

(error:LazyImageError?) in

//Image loaded. Do something..
}

With spinner and new scaled size. Image is resized for your desired size for maximum performance


let newSize = CGSize(width: imageViewWidth height: imageViewHeight)

self.lazyImage.showWithSpinner(imageView:self.imageView, url:"http://something.com/someimage.png", size:newSize) {

(error:LazyImageError?) in

//Image loaded. Do something..
}

Sometimes a specific URL can constantly change the corresponding image.

With completion


let newSize = CGSize(width: imageViewWidth height: imageViewHeight)

self.lazyImage.showOverride(imageView:self.imageView, url:"http://something.com/someimage.png", size:newSize) {

(error:LazyImageError?) in

//Image loaded. Do something..
}

With completion, spinner and new scaled size


let newSize = CGSize(width: imageViewWidth height: imageViewHeight)

self.lazyImage.showOverrideWithSpinner(imageView:self.imageView, url:"http://something.com/someimage.png", size:newSize) {

(error:LazyImageError?) in

//Image loaded. Do something..
}

Dealing with the image size and the cache

In general showing an image optimized for your view dimensions is the best approach for optimal memory handling . You can achieve this in 2 ways.


let newSize = CGSize(width: imageViewWidth height: imageViewHeight)

self.lazyImage.showWithSpinner(imageView:self.imageView, url:"http://something.com/someimage.png", size:newSize) {

(error:LazyImageError?) in

//Image loaded. Do something..
}

let imageSize = CGSize(width: yourViewWidth, height: yourViewHeight)



//Set default image size ratio

self.lazyImage.setCacheSize(imageSize)



self.lazyImage.showWithSpinner(imageView:self, url:imageURL) {

(error:LazyImageError?)  in

}

Clearing the cache for specific image URLs

Sometimes you just need to re-download a specific image with the exact same name once.

If you have a cache size set, clearing the cache will clear images of the specific size ratio.

Clearing the cache:


let imageURLs:[String] = ["https://someimage.png", "https://someotherimage.png"]

self.lazyImage.clearCacheForURLs(urls: urls)

//And you're done

Prefetching

You can prefetch an image and save it to be ready for fast displaying.


self.lazyImage.prefetchImage(url: "https://someimage.png")

Forget UIImageviews. Just get the UIImage


self.lazyImage.fetchImage(url: url) {

(image:UIImage?, error:LazyImageError?) in

//image has the UIImage

}

Cancel the image fetching request


self.lazyImage.cancel()

Contributing

Contributions are welcomed. Fork the project and make it better, then create a pull request. The project comes with a very basic target app for testing your new features. You can find the library code under Pods/Development Pods/LazyImage.

License

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0