CacheSerializer.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // CacheSerializer.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2016/09/02.
  6. //
  7. // Copyright (c) 2019 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: KFCrossPlatformImage, original: Data?) -> Data?
  42. /// Gets an image from provided serialized data.
  43. ///
  44. /// - Parameters:
  45. /// - data: The data from which an image should be deserialized.
  46. /// - options: The parsed 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: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage?
  50. /// Gets an image deserialized from provided data.
  51. ///
  52. /// - Parameters:
  53. /// - data: The data from which an image should be deserialized.
  54. /// - options: Options for deserialization.
  55. /// - Returns: An image deserialized or `nil` when no valid image
  56. /// could be deserialized.
  57. /// - Note:
  58. /// This method is deprecated. Please implement the version with
  59. /// `KingfisherParsedOptionsInfo` as parameter instead.
  60. @available(*, deprecated,
  61. message: "Deprecated. Implement the method with same name but with `KingfisherParsedOptionsInfo` instead.")
  62. func image(with data: Data, options: KingfisherOptionsInfo?) -> KFCrossPlatformImage?
  63. }
  64. extension CacheSerializer {
  65. public func image(with data: Data, options: KingfisherOptionsInfo?) -> KFCrossPlatformImage? {
  66. return image(with: data, options: KingfisherParsedOptionsInfo(options))
  67. }
  68. }
  69. /// Represents a basic and default `CacheSerializer` used in Kingfisher disk cache system.
  70. /// It could serialize and deserialize images in PNG, JPEG and GIF format. For
  71. /// image other than these formats, a normalized `pngRepresentation` will be used.
  72. public struct DefaultCacheSerializer: CacheSerializer {
  73. /// The default general cache serializer used across Kingfisher's cache.
  74. public static let `default` = DefaultCacheSerializer()
  75. private init() {}
  76. /// - Parameters:
  77. /// - image: The image needed to be serialized.
  78. /// - original: The original data which is just downloaded.
  79. /// If the image is retrieved from cache instead of
  80. /// downloaded, it will be `nil`.
  81. /// - Returns: The data object for storing to disk, or `nil` when no valid
  82. /// data could be serialized.
  83. ///
  84. /// - Note:
  85. /// Only when `original` contains valid PNG, JPEG and GIF format data, the `image` will be
  86. /// converted to the corresponding data type. Otherwise, if the `original` is provided but it is not
  87. /// If `original` is `nil`, the input `image` will be encoded as PNG data.
  88. public func data(with image: KFCrossPlatformImage, original: Data?) -> Data? {
  89. return image.kf.data(format: original?.kf.imageFormat ?? .unknown)
  90. }
  91. /// Gets an image deserialized from provided data.
  92. ///
  93. /// - Parameters:
  94. /// - data: The data from which an image should be deserialized.
  95. /// - options: Options for deserialization.
  96. /// - Returns: An image deserialized or `nil` when no valid image
  97. /// could be deserialized.
  98. public func image(with data: Data, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
  99. return KingfisherWrapper.image(data: data, options: options.imageCreatingOptions)
  100. }
  101. }