Search code examples
swift3kingfisher

Kingfisher and swift 3 - cannot set image with url


Checking the documentation and the migration guide, I should be able to set a new image using this code:

imageView.kf.setImage(with:url ...) 

but actually I cannot find this method in the library, I only see:

imageView.kf.setImage(with:Resource... )

I don't know exactly how this resource shoud work though since I cannot find anything in the documentation.


Solution

  • Resource is a protocol. URL has been extended to conform to this protocol. So you can do:

    let url = URL(string: ...)!
    imageView.kf.setImage(with: url)
    

    If you want some control over what Kingfisher uses for the key in its cache, you can use ImageResource:

    let identifier = "..."
    let url = URL(string: "http://example.com/images/identifier=\(identifier)")!
    let resource = ImageResource(downloadURL: url, cacheKey: identifier)
    
    imageView.kf.setImage(with: resource)