2
0

CacheSerializer.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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` is used to convert some data to an image object after
  28. /// retrieving it from disk storage, and vice versa, to convert an image to data object
  29. /// for storing to the disk storage.
  30. public protocol CacheSerializer {
  31. /// Gets the serialized data from a provided image
  32. /// and optional original data for caching to disk.
  33. ///
  34. /// - Parameters:
  35. /// - image: The image needed to be serialized.
  36. /// - original: The original data which is just downloaded.
  37. /// If the image is retrieved from cache instead of
  38. /// downloaded, it will be `nil`.
  39. /// - Returns: The data object for storing to disk, or `nil` when no valid
  40. /// data could be serialized.
  41. func data(with image: Image, original: Data?) -> Data?
  42. /// Gets an image deserialized from provided data.
  43. ///
  44. /// - Parameters:
  45. /// - data: The data from which an image should be deserialized.
  46. /// - options: Options for deserialization.
  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. /// Represents a basic and default `CacheSerializer` used in Kingfisher disk cache system.
  52. /// It could serialize and deserialize images in PNG, JEPG and GIF format. For
  53. /// image other than these formats, a normalized `pngRepresentation` will be used.
  54. public struct DefaultCacheSerializer: CacheSerializer {
  55. /// The default general cache serializer used across Kingfisher's cache.
  56. public static let `default` = DefaultCacheSerializer()
  57. private init() {}
  58. /// - Parameters:
  59. /// - image: The image needed to be serialized.
  60. /// - original: The original data which is just downloaded.
  61. /// If the image is retrieved from cache instead of
  62. /// downloaded, it will be `nil`.
  63. /// - Returns: The data object for storing to disk, or `nil` when no valid
  64. /// data could be serialized.
  65. ///
  66. /// - Note:
  67. /// Only when `original` contains valid PNG, JEPG and GIF format data, the `image` will be
  68. /// converted to the corresponding data type. Otherwise, if the `original` is provided but it is not
  69. /// a valid format, the `original` data will be used for cache.
  70. ///
  71. /// If `original` is `nil`, the input `image` will be encoded as PNG data.
  72. public func data(with image: Image, original: Data?) -> Data? {
  73. let imageFormat = original?.kf.imageFormat ?? .unknown
  74. let data: Data?
  75. switch imageFormat {
  76. case .PNG: data = image.kf.pngRepresentation()
  77. case .JPEG: data = image.kf.jpegRepresentation(compressionQuality: 1.0)
  78. case .GIF: data = image.kf.gifRepresentation()
  79. case .unknown: data = original ?? image.kf.normalized.kf.pngRepresentation()
  80. }
  81. return data
  82. }
  83. /// Gets an image deserialized from provided data.
  84. ///
  85. /// - Parameters:
  86. /// - data: The data from which an image should be deserialized.
  87. /// - options: Options for deserialization.
  88. /// - Returns: An image deserialized or `nil` when no valid image
  89. /// could be deserialized.
  90. public func image(with data: Data, options: KingfisherOptionsInfo?) -> Image? {
  91. let options = options ?? .empty
  92. return KingfisherClass.image(data: data, options: options.imageCreatingOptions)
  93. }
  94. }