CacheSerializer.swift 6.0 KB

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