CacheSerializer.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /// A `CacheSerializer` is used to convert some data to an image object after retrieving it from disk storage,
  34. /// and vice versa, to convert an image to a data object for storing it to the disk storage.
  35. public protocol CacheSerializer {
  36. /// Retrieves the serialized data from a provided image and optional original data for caching to disk.
  37. ///
  38. /// - Parameters:
  39. /// - image: The image to be serialized.
  40. /// - original: The original data that was just downloaded.
  41. /// If the image is retrieved from the cache instead of being downloaded, it will be `nil`.
  42. /// - Returns: The data object for storing to disk, or `nil` when no valid data can be serialized.
  43. func data(with image: KFCrossPlatformImage, original: Data?) -> Data?
  44. /// Retrieves an image from the provided serialized data.
  45. ///
  46. /// - Parameters:
  47. /// - data: The data from which an image should be deserialized.
  48. /// - options: The parsed options for deserialization.
  49. /// - Returns: A deserialized image, or `nil` when no valid image can be deserialized.
  50. func image(with data: Data, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage?
  51. /// Indicates whether this serializer prefers to cache the original data in its implementation.
  52. ///
  53. /// If `true`, during storing phase, the original data is preferred to be stored to the disk if exists. When
  54. /// retrieving image from the disk cache, after creating the image from the loaded data, Kingfisher will continue
  55. /// to apply the processor to get the final image.
  56. ///
  57. /// By default, it is `false`, and the actual processed image is assumed to be serialized to and later deserialized
  58. /// from the disk. That means the processed version of the image is stored and loaded.
  59. var originalDataUsed: Bool { get }
  60. }
  61. public extension CacheSerializer {
  62. var originalDataUsed: Bool { false }
  63. }
  64. /// Represents a basic and default `CacheSerializer` used in the Kingfisher disk cache system.
  65. ///
  66. /// It can serialize and deserialize images in PNG, JPEG, and GIF formats. For images other than these formats, a
  67. /// normalized ``KingfisherWrapper/pngRepresentation()`` will be used.
  68. ///
  69. /// When converting an `image` to the date, it will only be converted to the corresponding data type when `original`
  70. /// contains valid PNG, JPEG, and GIF format data. If the `original` is provided but not valid, or if `original` is
  71. /// `nil`, the input `image` will be encoded as PNG data.
  72. public struct DefaultCacheSerializer: CacheSerializer {
  73. /// The default general cache serializer utilized throughout Kingfisher's caching mechanism.
  74. public static let `default` = DefaultCacheSerializer()
  75. /// The compression quality used when converting an image to lossy format data (such as JPEG).
  76. ///
  77. /// Default is 1.0.
  78. public var compressionQuality: CGFloat = 1.0
  79. /// Determines whether the original data should be prioritized during image serialization.
  80. ///
  81. /// If set to `true`, the original input data will be initially inspected and used, unless the data is `nil`.
  82. /// In the event of a `nil` data, the serialization process will revert to generating data from the image.
  83. ///
  84. /// > This value is used as ``CacheSerializer/originalDataUsed-d2v9``.
  85. public var preferCacheOriginalData: Bool = false
  86. public var originalDataUsed: Bool { preferCacheOriginalData }
  87. /// Creates a cache serializer that serializes and deserializes images in PNG, JPEG, and GIF formats.
  88. ///
  89. /// > Prefer to use the ``DefaultCacheSerializer/default`` value unless you need to specify your own properties.
  90. public init() { }
  91. public func data(with image: KFCrossPlatformImage, original: Data?) -> Data? {
  92. if preferCacheOriginalData {
  93. return original ??
  94. image.kf.data(
  95. format: original?.kf.imageFormat ?? .unknown,
  96. compressionQuality: compressionQuality
  97. )
  98. } else {
  99. return image.kf.data(
  100. format: original?.kf.imageFormat ?? .unknown,
  101. compressionQuality: compressionQuality
  102. )
  103. }
  104. }
  105. public func image(with data: Data, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
  106. return KingfisherWrapper.image(data: data, options: options.imageCreatingOptions)
  107. }
  108. }