Using SDWebImage for WebP Images in Swift

Posted By : Akshay Luthra | 31-Mar-2016

The very first questions in mind when I heard about the word “WebP” were : What is WebP ? Why should I use it ?

 

The answer is pretty simple :

WebP is a modern image format that provides superior lossless and lossy compression for images on the web. Using WebP, can create smaller, richer images that make the overall experience of using an app faster.

We can use WebP format images in place of the PNG or JPEG format images in the projects to make the usability experience much faster.

 

The UIImageView in Swift doesn’t directly support WebP format images. Therefore, there is a need of a library to use in the iOS Swift projects to make use of the WebP images.

 

SDWebImage is a very good iOS library that can be used to support WebP format images in iOS projects.

 

SDWebImage provides Asynchronous image downloader with cache support as a UIImageView category for the WebP images.

The SDWebImage also supports PNG and JPEG format images, so you do not have to worry much while using it.

 

 

SDWebImage library provides a category for UIImageView with support for remote images coming from the web :

- An UIImageView category adding web image and cache management to the Cocoa Touch framework

- An asynchronous image downloader

- An asynchronous memory + disk image caching with automatic cache expiration handling- 

WebP format support

A guarantee that the same URL won't be downloaded several times

A guarantee that bogus URLs won't be retried again and again

A guarantee that main thread will never be blocked

Performances!

 

 

To use the SDWebImage library in your Swift project, you need to install the SDWebImage dependency in your project.

I’m fond of using CocoaPods, so I’ll details the steps of how to install the SDWebImage dependency using CocoaPods :

1) Open Terminal

2) cd /<PATH_TO_SWIFT_PROJECT>

3) vim Podfile

4) In the Podfile, write the following to add the SDWebImage dependency 

 

platform :ios, '8.0’ 

use_frameworks!

pod 'SDWebImage/WebP’

 

Save it and quit from the Podfile

 

5) pod install

 

 

After installing the SDWebImage dependency through CocoaPods, you can use it in your Swift files by impoting it :

import SDWebImage

 

 

Using SDWebImage Asynchronous Image Downloader in your Swift code :

var manager:SDWebImageManager = SDWebImageManager.sharedManager()
self.manager.downloadImageWithURL(imageURL,
  options: SDWebImageOptions.HighPriority,
  progress: nil,
  completed: {[weak self] (image, error, cached, finished, url) in
    if (error == nil && image && finished) {
      // do something with image
    }
})
 

 

Using SDWebImage Asynchronous Image Caching Independently in your Swift code :

var imageCache: SDImageCache = SDImageCache(namespace: "XXXXXXX")
imageCache.queryDiskCacheForKey(myCacheKey, done: { (image, cacheType) -> Void in
   // image is not nil if image was found          
})
 

 

To store an image into the cache, you use the storeImage:forKey: method :

SDImageCache.sharedImageCache().storeImage(image, forKey: myCacheKey, toDisk: true)
 

 

Hope this blog is useful to you.

Thanks for reading it!

 

About Author

Author Image
Akshay Luthra

Akshay Luthra has excellent experience in developing Cross Platform Mobile Apps using JavaScript and Titanium Framework. He loves listening to music and travelling new places.

Request for Proposal

Name is required

Comment is required

Sending message..