CacheSerializer.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // CacheSerializer.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2016/09/02.
  6. //
  7. // Copyright (c) 2018 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. /// An `CacheSerializer` would be used to convert some data to an image object for
  28. /// retrieving from disk cache and vice versa for storing to disk cache.
  29. public protocol CacheSerializer {
  30. /// Get the serialized data from a provided image
  31. /// and optional original data for caching to disk.
  32. ///
  33. ///
  34. /// - parameter image: The image needed to be serialized.
  35. /// - parameter original: The original data which is just downloaded.
  36. /// If the image is retrieved from cache instead of
  37. /// downloaded, it will be `nil`.
  38. ///
  39. /// - returns: A data which will be stored to cache, or `nil` when no valid
  40. /// data could be serialized.
  41. func data(with image: Image, original: Data?) -> Data?
  42. /// Get an image deserialized from provided data.
  43. ///
  44. /// - parameter data: The data from which an image should be deserialized.
  45. /// - parameter options: Options for deserialization.
  46. ///
  47. /// - returns: An image deserialized or `nil` when no valid image
  48. /// could be deserialized.
  49. func image(with data: Data, options: KingfisherOptionsInfo?) -> Image?
  50. }
  51. /// `DefaultCacheSerializer` is a basic `CacheSerializer` used in default cache of
  52. /// Kingfisher. It could serialize and deserialize PNG, JEPG and GIF images. For
  53. /// image other than these formats, a normalized `pngRepresentation` will be used.
  54. public struct DefaultCacheSerializer: CacheSerializer {
  55. public static let `default` = DefaultCacheSerializer()
  56. private init() {}
  57. public func data(with image: Image, original: Data?) -> Data? {
  58. let imageFormat = original?.kf.imageFormat ?? .unknown
  59. let data: Data?
  60. switch imageFormat {
  61. case .PNG: data = image.kf.pngRepresentation()
  62. case .JPEG: data = image.kf.jpegRepresentation(compressionQuality: 1.0)
  63. case .GIF: data = image.kf.gifRepresentation()
  64. case .unknown: data = original ?? image.kf.normalized.kf.pngRepresentation()
  65. }
  66. return data
  67. }
  68. public func image(with data: Data, options: KingfisherOptionsInfo?) -> Image? {
  69. let options = options ?? KingfisherEmptyOptionsInfo
  70. return Kingfisher<Image>.image(
  71. data: data,
  72. scale: options.scaleFactor,
  73. preloadAllAnimationData: options.preloadAllAnimationData,
  74. onlyFirstFrame: options.onlyLoadFirstFrame)
  75. }
  76. }