You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
6.2 KiB

  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. import CoreGraphics
  28. /// An `CacheSerializer` is used to convert some data to an image object after
  29. /// retrieving it from disk storage, and vice versa, to convert an image to data object
  30. /// for storing to the disk storage.
  31. public protocol CacheSerializer {
  32. /// Gets the serialized data from a provided image
  33. /// and optional original data for caching to disk.
  34. ///
  35. /// - Parameters:
  36. /// - image: The image needed to be serialized.
  37. /// - original: The original data which is just downloaded.
  38. /// If the image is retrieved from cache instead of
  39. /// downloaded, it will be `nil`.
  40. /// - Returns: The data object for storing to disk, or `nil` when no valid
  41. /// data could be serialized.
  42. func data(with image: KFCrossPlatformImage, original: Data?) -> Data?
  43. /// Gets an image from provided serialized data.
  44. ///
  45. /// - Parameters:
  46. /// - data: The data from which an image should be deserialized.
  47. /// - options: The parsed options for deserialization.
  48. /// - Returns: An image deserialized or `nil` when no valid image
  49. /// could be deserialized.
  50. func image(with data: Data, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage?
  51. /// Gets an image deserialized from provided data.
  52. ///
  53. /// - Parameters:
  54. /// - data: The data from which an image should be deserialized.
  55. /// - options: Options for deserialization.
  56. /// - Returns: An image deserialized or `nil` when no valid image
  57. /// could be deserialized.
  58. /// - Note:
  59. /// This method is deprecated. Please implement the version with
  60. /// `KingfisherParsedOptionsInfo` as parameter instead.
  61. @available(*, deprecated,
  62. message: "Deprecated. Implement the method with same name but with `KingfisherParsedOptionsInfo` instead.")
  63. func image(with data: Data, options: KingfisherOptionsInfo?) -> KFCrossPlatformImage?
  64. }
  65. extension CacheSerializer {
  66. public func image(with data: Data, options: KingfisherOptionsInfo?) -> KFCrossPlatformImage? {
  67. return image(with: data, options: KingfisherParsedOptionsInfo(options))
  68. }
  69. }
  70. /// Represents a basic and default `CacheSerializer` used in Kingfisher disk cache system.
  71. /// It could serialize and deserialize images in PNG, JPEG and GIF format. For
  72. /// image other than these formats, a normalized `pngRepresentation` will be used.
  73. public struct DefaultCacheSerializer: CacheSerializer {
  74. /// The default general cache serializer used across Kingfisher's cache.
  75. public static let `default` = DefaultCacheSerializer()
  76. /// The compression quality when converting image to a lossy format data. Default is 1.0.
  77. public var compressionQuality: CGFloat = 1.0
  78. /// Whether the original data should be preferred when serializing the image.
  79. /// If `true`, the input original data will be checked first and used unless the data is `nil`.
  80. /// In that case, the serialization will fall back to creating data from image.
  81. public var preferCacheOriginalData: Bool = false
  82. /// Creates a cache serializer that serialize and deserialize images in PNG, JPEG and GIF format.
  83. ///
  84. /// - Note:
  85. /// Use `DefaultCacheSerializer.default` unless you need to specify your own properties.
  86. ///
  87. public init() { }
  88. /// - Parameters:
  89. /// - image: The image needed to be serialized.
  90. /// - original: The original data which is just downloaded.
  91. /// If the image is retrieved from cache instead of
  92. /// downloaded, it will be `nil`.
  93. /// - Returns: The data object for storing to disk, or `nil` when no valid
  94. /// data could be serialized.
  95. ///
  96. /// - Note:
  97. /// Only when `original` contains valid PNG, JPEG and GIF format data, the `image` will be
  98. /// converted to the corresponding data type. Otherwise, if the `original` is provided but it is not
  99. /// If `original` is `nil`, the input `image` will be encoded as PNG data.
  100. public func data(with image: KFCrossPlatformImage, original: Data?) -> Data? {
  101. if preferCacheOriginalData {
  102. return original ??
  103. image.kf.data(
  104. format: original?.kf.imageFormat ?? .unknown,
  105. compressionQuality: compressionQuality
  106. )
  107. } else {
  108. return image.kf.data(
  109. format: original?.kf.imageFormat ?? .unknown,
  110. compressionQuality: compressionQuality
  111. )
  112. }
  113. }
  114. /// Gets an image deserialized from provided data.
  115. ///
  116. /// - Parameters:
  117. /// - data: The data from which an image should be deserialized.
  118. /// - options: Options for deserialization.
  119. /// - Returns: An image deserialized or `nil` when no valid image
  120. /// could be deserialized.
  121. public func image(with data: Data, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
  122. return KingfisherWrapper.image(data: data, options: options.imageCreatingOptions)
  123. }
  124. }