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
UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.
Kingfisher is a powerful, pure-Swift library for downloading and caching images from the web. It provides you a chance to use a pure-Swift way to work with remote images in your next app.
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) })
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With