iosswiftkingfisher

Kingfisher: Ambiguous use of 'setBackgroundImage(with:for:placeholder:options:progressBlock:completionHandler:)'


I've been trying to use Kingfisher to set the background image of a button, but I'm getting a swift compiler error:

Ambiguous use of 'setBackgroundImage(with:for:placeholder:options:progressBlock:completionHandler:)'

What about this expression is ambiguous? I looked at the KF documentation and I think this is how you call it.

var options: KingfisherOptionsInfo = []
options.append(.forceRefresh)
button.kf.setBackgroundImage(with: URL(string: picture), for: .normal, placeholder: nil, options: options, progressBlock: nil, completionHandler: nil)

Solution

  • That error is because you need to handle the completionHandler instead of passing nil. Try below code:

    button.kf.setBackgroundImage(with: URL(string: picture), for: .normal, placeholder: nil, options: options, progressBlock: nil) { result in
        // result is either a .success(RetrieveImageResult) or a .failure(KingfisherError)
        switch result {
        case .success(let value):
            // The image was set to image view:
            print(value.image)
    
            // From where the image was retrieved:
            // - .none - Just downloaded.
            // - .memory - Got from memory cache.
            // - .disk - Got from disk cache.
            print(value.cacheType)
    
            // The source object which contains information like `url`.
            print(value.source)
    
        case .failure(let error):
            print(error) // The error happens
        }
    }