Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 432
MatterGoal Avatar asked Dec 01 '25 09:12

MatterGoal


2 Answers

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)
like image 122
Rob Avatar answered Dec 04 '25 04:12

Rob


For Swift 4.2

import Kingfisher

extension UIImageView {
    func setImage(with urlString: String){
        guard let url = URL(string: urlString) else {
            return
        }
        let resource = ImageResource(downloadURL: url, cacheKey: urlString)
        var kf = self.kf
        kf.indicatorType = .activity
        self.kf.setImage(with: resource)
    }
}

How to use

self.imgVw.setImage(with: your image url)
like image 21
Hardik Thakkar Avatar answered Dec 04 '25 03:12

Hardik Thakkar