123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import Foundation
- public protocol CacheSerializer {
-
-
-
-
-
-
-
-
-
-
-
-
- func data(with image: Image, original: Data?) -> Data?
-
-
-
-
-
-
-
-
- func image(with data: Data, options: KingfisherOptionsInfo?) -> Image?
- }
- public struct DefaultCacheSerializer: CacheSerializer {
-
- public static let `default` = DefaultCacheSerializer()
- private init() {}
-
- public func data(with image: Image, original: Data?) -> Data? {
- let imageFormat = original?.kf.imageFormat ?? .unknown
- let data: Data?
- switch imageFormat {
- case .PNG: data = image.kf.pngRepresentation()
- case .JPEG: data = image.kf.jpegRepresentation(compressionQuality: 1.0)
- case .GIF: data = image.kf.gifRepresentation()
- case .unknown: data = original ?? image.kf.normalized.kf.pngRepresentation()
- }
- return data
- }
-
- public func image(with data: Data, options: KingfisherOptionsInfo?) -> Image? {
- let options = options ?? KingfisherEmptyOptionsInfo
- return Kingfisher<Image>.image(
- data: data,
- scale: options.scaleFactor,
- preloadAllAnimationData: options.preloadAllAnimationData,
- onlyFirstFrame: options.onlyLoadFirstFrame)
- }
- }
|