CacheSerializer.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. }
  52. /// Represents a basic and default `CacheSerializer` used in Kingfisher disk cache system.
  53. /// It could serialize and deserialize images in PNG, JPEG and GIF format. For
  54. /// image other than these formats, a normalized `pngRepresentation` will be used.
  55. public struct DefaultCacheSerializer: CacheSerializer {
  56. /// The default general cache serializer used across Kingfisher's cache.
  57. public static let `default` = DefaultCacheSerializer()
  58. /// The compression quality when converting image to a lossy format data. Default is 1.0.
  59. public var compressionQuality: CGFloat = 1.0
  60. /// Whether the original data should be preferred when serializing the image.
  61. /// If `true`, the input original data will be checked first and used unless the data is `nil`.
  62. /// In that case, the serialization will fall back to creating data from image.
  63. public var preferCacheOriginalData: Bool = false
  64. /// Creates a cache serializer that serialize and deserialize images in PNG, JPEG and GIF format.
  65. ///
  66. /// - Note:
  67. /// Use `DefaultCacheSerializer.default` unless you need to specify your own properties.
  68. ///
  69. public init() { }
  70. /// - Parameters:
  71. /// - image: The image needed to be serialized.
  72. /// - original: The original data which is just downloaded.
  73. /// If the image is retrieved from cache instead of
  74. /// downloaded, it will be `nil`.
  75. /// - Returns: The data object for storing to disk, or `nil` when no valid
  76. /// data could be serialized.
  77. ///
  78. /// - Note:
  79. /// Only when `original` contains valid PNG, JPEG and GIF format data, the `image` will be
  80. /// converted to the corresponding data type. Otherwise, if the `original` is provided but it is not
  81. /// If `original` is `nil`, the input `image` will be encoded as PNG data.
  82. public func data(with image: KFCrossPlatformImage, original: Data?) -> Data? {
  83. if preferCacheOriginalData {
  84. return original ??
  85. image.kf.data(
  86. format: original?.kf.imageFormat ?? .unknown,
  87. compressionQuality: compressionQuality
  88. )
  89. } else {
  90. return image.kf.data(
  91. format: original?.kf.imageFormat ?? .unknown,
  92. compressionQuality: compressionQuality
  93. )
  94. }
  95. }
  96. /// Gets an image deserialized from provided data.
  97. ///
  98. /// - Parameters:
  99. /// - data: The data from which an image should be deserialized.
  100. /// - options: Options for deserialization.
  101. /// - Returns: An image deserialized or `nil` when no valid image
  102. /// could be deserialized.
  103. public func image(with data: Data, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
  104. return KingfisherWrapper.image(data: data, options: options.imageCreatingOptions)
  105. }
  106. }