Skip to content

Instantly share code, notes, and snippets.

@stleamist
Created June 16, 2020 09:25
Show Gist options
  • Save stleamist/303000102ef948f098eacc5633379574 to your computer and use it in GitHub Desktop.
Save stleamist/303000102ef948f098eacc5633379574 to your computer and use it in GitHub Desktop.
import Foundation
import Kingfisher
import FavIcon
struct KFFaviconProvider: ImageDataProvider {
let url: URL
let cacheKey: String
init(url: URL, cacheKey: String? = nil) {
self.url = url
self.cacheKey = cacheKey ?? url.absoluteString
}
func data(handler: @escaping (Result<Data, Error>) -> Void) {
do {
try FavIcon.downloadPreferred(url) { result in
switch result {
case .success(let image):
guard let data = image.pngData() ?? image.jpegData(compressionQuality: 1) else {
return
}
handler(.success(data))
case .failure(let error):
handler(.failure(error))
}
}
} catch {
handler(.failure(error))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment