Created
June 16, 2020 09:25
-
-
Save stleamist/303000102ef948f098eacc5633379574 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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