FormatIndicatedCacheSerializer.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // RequestModifier.swift
  3. // Kingfisher
  4. //
  5. // Created by Junyu Kuang on 5/28/17.
  6. //
  7. // Copyright (c) 2018 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. /// `FormatIndicatedCacheSerializer` let you indicate an image format for serialized caches.
  28. ///
  29. /// It could serialize and deserialize PNG, JEPG and GIF images. For
  30. /// image other than these formats, a normalized `pngRepresentation` will be used.
  31. ///
  32. /// Example:
  33. /// ````
  34. /// private let profileImageSize = CGSize(width: 44, height: 44)
  35. ///
  36. /// private let imageProcessor = RoundCornerImageProcessor(
  37. /// cornerRadius: profileImageSize.width / 2, targetSize: profileImageSize)
  38. ///
  39. /// private let optionsInfo: KingfisherOptionsInfo = [
  40. /// .cacheSerializer(FormatIndicatedCacheSerializer.png),
  41. /// .backgroundDecode, .processor(imageProcessor), .scaleFactor(UIScreen.main.scale)]
  42. ///
  43. /// extension UIImageView {
  44. /// func setProfileImage(with url: URL) {
  45. /// // Image will always cached as PNG format to preserve alpha channel for round rect.
  46. /// _ = kf.setImage(with: url, options: optionsInfo)
  47. /// }
  48. ///}
  49. /// ````
  50. public struct FormatIndicatedCacheSerializer: CacheSerializer {
  51. public static let png = FormatIndicatedCacheSerializer(imageFormat: .PNG)
  52. public static let jpeg = FormatIndicatedCacheSerializer(imageFormat: .JPEG)
  53. public static let gif = FormatIndicatedCacheSerializer(imageFormat: .GIF)
  54. /// The indicated image format.
  55. private let imageFormat: ImageFormat
  56. public func data(with image: Image, original: Data?) -> Data? {
  57. func imageData(withFormat imageFormat: ImageFormat) -> Data? {
  58. switch imageFormat {
  59. case .PNG: return image.kf.pngRepresentation()
  60. case .JPEG: return image.kf.jpegRepresentation(compressionQuality: 1.0)
  61. case .GIF: return image.kf.gifRepresentation()
  62. case .unknown: return nil
  63. }
  64. }
  65. // generate data with indicated image format
  66. if let data = imageData(withFormat: imageFormat) {
  67. return data
  68. }
  69. let originalFormat = original?.kf.imageFormat ?? .unknown
  70. // generate data with original image's format
  71. if originalFormat != imageFormat, let data = imageData(withFormat: originalFormat) {
  72. return data
  73. }
  74. return original ?? image.kf.normalized.kf.pngRepresentation()
  75. }
  76. /// Same implementation as `DefaultCacheSerializer`.
  77. public func image(with data: Data, options: KingfisherOptionsInfo?) -> Image? {
  78. let options = options ?? KingfisherEmptyOptionsInfo
  79. return Kingfisher<Image>.image(
  80. data: data,
  81. scale: options.scaleFactor,
  82. preloadAllAnimationData: options.preloadAllAnimationData,
  83. onlyFirstFrame: options.onlyLoadFirstFrame)
  84. }
  85. }