Skip to content

Instantly share code, notes, and snippets.

@andreconghau
Last active October 14, 2020 06:36
Show Gist options
  • Save andreconghau/4c3b04205195f452800d2892e91a079a to your computer and use it in GitHub Desktop.
Save andreconghau/4c3b04205195f452800d2892e91a079a to your computer and use it in GitHub Desktop.
Kingfisher download image and caching in memory. can be show progress bar downloading
// https://github.com/onevcat/Kingfisher/wiki/Installation-Guide
// https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet
import UIKit
import Kingfisher
class example {
func viewDidLoad {
// Apply KingFisher
let resource = ImageResource(downloadURL: imgUrl!)
let placeholder = UIImage(named: "non-avatar")
let processor = RoundCornerImageProcessor(cornerRadius: 20)
cell.avatar.kf.indicatorType = .activity
cell.avatar.kf.setImage(with: resource, placeholder: placeholder, options: [.processor(processor)], progressBlock: { receivedSize, totalSize in
let percentage = (Float(receivedSize) / Float(totalSize)) * 100.0
print("downloading progress: \(percentage)%")
}) { (result) in
self.handle(result)
}
}
func handle(_ result: Result<RetrieveImageResult, KingfisherError>) {
switch result {
case .success(let retrieveImageResult):
print("sucess")
let image = retrieveImageResult.image
let cacheType = retrieveImageResult.cacheType
let source = retrieveImageResult.source
let originalSource = retrieveImageResult.originalSource
let message = """
Image Size:
\(image.size)
Cache:
\(cacheType)
Source:
\(source)
Original source:
\(originalSource)
"""
print(message)
case.failure(let kingfisherError):
print(kingfisherError)
}
}
}
sucess
Image Size:
(460.0, 460.0)
Cache:
disk
Source:
network(Kingfisher.ImageResource(cacheKey: "https://avatars0.githubusercontent.com/u/5936?v=4", downloadURL: https://avatars0.githubusercontent.com/u/5936?v=4))
Original source:
network(Kingfisher.ImageResource(cacheKey: "https://avatars0.githubusercontent.com/u/5936?v=4", downloadURL: https://avatars0.githubusercontent.com/u/5936?v=4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment