Get UIImage only with Kingfisher library

Solution 1:

You could use the retrieveImage(with:options:progressBlock: completionHandler:) method of KingfisherManager for this.

Maybe something like this:

KingfisherManager.shared.retrieveImage(with: url, options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in
    print(image)
})

Solution 2:

This is latest syntax to download image in kingFisher 5 (Tested in swift 4.2)

func downloadImage(`with` urlString : String){
    guard let url = URL.init(string: urlString) else {
        return
    }
    let resource = ImageResource(downloadURL: url)

    KingfisherManager.shared.retrieveImage(with: resource, options: nil, progressBlock: nil) { result in
        switch result {
        case .success(let value):
            print("Image: \(value.image). Got from: \(value.cacheType)")
        case .failure(let error):
            print("Error: \(error)")
        }
    }
}

How to call above function

self.downloadImage(with: imageURL) //replace with your image url

Solution 3:

With the new Swift 3 version you can use ImageDownloader :

ImageDownloader.default.downloadImage(with: url, options: [], progressBlock: nil) {
    (image, error, url, data) in
    print("Downloaded Image: \(image)")
}

ImageDownloader is a part of Kingfisher so don't forget to import Kingfisher.

Solution 4:

Swift 5:

I will complete on @Hardik Thakkar answer to add a change so that you can return the image using a closure may that helps somebody:

func downloadImage(with urlString : String , imageCompletionHandler: @escaping (UIImage?) -> Void){
        guard let url = URL.init(string: urlString) else {
            return  imageCompletionHandler(nil)
        }
        let resource = ImageResource(downloadURL: url)
        
        KingfisherManager.shared.retrieveImage(with: resource, options: nil, progressBlock: nil) { result in
            switch result {
            case .success(let value):
                imageCompletionHandler(value.image)
            case .failure:
                imageCompletionHandler(nil)
            }
        }
    }

How to call:

 downloadImage(with :yourUrl){image in
     guard let image  = image else { return}
      // do what you need with the returned image.
 }

Solution 5:

Another way in Kingfisher 5:

KingfisherManager.shared.retrieveImage(with: url) { result in
    let image = try? result.get().image
    if let image = image {
        ...
    }
}