Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UIImage only with Kingfisher library

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

like image 389
Patonz Avatar asked Feb 04 '16 15:02

Patonz


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.

What is Onevcat Kingfisher?

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.


2 Answers

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) }) 
like image 122
onevcat Avatar answered Sep 17 '22 01:09

onevcat


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 
like image 20
Hardik Thakkar Avatar answered Sep 19 '22 01:09

Hardik Thakkar