Get UIImage only with Kingfisher library

Patonz picture Patonz · Feb 4, 2016 · Viewed 30.2k times · Source

I need to get a UIImage only instead of loading a normal UIImageView with Kingfisher library

To realize it I implemented a workaround with UIImageView:

let imageView = UIImageView()

imageView.kf_setImageWithURL(NSURL(string: cpa.imageName)!, placeholderImage: nil,
        optionsInfo: [.Transition(ImageTransition.Fade(1))],
        progressBlock: { receivedSize, totalSize in
            print("\(receivedSize)/\(totalSize)")
        },
        completionHandler: { image, error, cacheType, imageURL in
            anView!.image = image //anView IS NOT an UIImageView
            anView!.frame.size = CGSize(width: 15.0, height: 15.0)
            print("Finished")
    })

This code works perfectly, but I would like to do it in a cleaner way. Is there a method in this library to get an UIImage only? Async and cached

Answer

onevcat picture onevcat · Mar 10, 2016

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)
})