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.

102 lines
4.6 KiB

  1. //
  2. // RequestModifier.swift
  3. // Kingfisher
  4. //
  5. // Created by Junyu Kuang on 5/28/17.
  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. /// `FormatIndicatedCacheSerializer` lets you indicate an image format for serialized caches.
  28. ///
  29. /// It could serialize and deserialize PNG, JPEG and GIF images. For
  30. /// image other than these formats, a normalized `pngRepresentation` will be used.
  31. ///
  32. /// Example:
  33. /// ````
  34. /// let profileImageSize = CGSize(width: 44, height: 44)
  35. ///
  36. /// // A round corner image.
  37. /// let imageProcessor = RoundCornerImageProcessor(
  38. /// cornerRadius: profileImageSize.width / 2, targetSize: profileImageSize)
  39. ///
  40. /// let optionsInfo: KingfisherOptionsInfo = [
  41. /// .cacheSerializer(FormatIndicatedCacheSerializer.png),
  42. /// .processor(imageProcessor)]
  43. ///
  44. /// A URL pointing to a JPEG image.
  45. /// let url = URL(string: "https://example.com/image.jpg")!
  46. ///
  47. /// // Image will be always cached as PNG format to preserve alpha channel for round rectangle.
  48. /// // So when you load it from cache again later, it will be still round cornered.
  49. /// // Otherwise, the corner part would be filled by white color (since JPEG does not contain an alpha channel).
  50. /// imageView.kf.setImage(with: url, options: optionsInfo)
  51. /// ````
  52. public struct FormatIndicatedCacheSerializer: CacheSerializer {
  53. /// A `FormatIndicatedCacheSerializer` which converts image from and to PNG format. If the image cannot be
  54. /// represented by PNG format, it will fallback to its real format which is determined by `original` data.
  55. public static let png = FormatIndicatedCacheSerializer(imageFormat: .PNG)
  56. /// A `FormatIndicatedCacheSerializer` which converts image from and to JPEG format. If the image cannot be
  57. /// represented by JPEG format, it will fallback to its real format which is determined by `original` data.
  58. public static let jpeg = FormatIndicatedCacheSerializer(imageFormat: .JPEG)
  59. /// A `FormatIndicatedCacheSerializer` which converts image from and to GIF format. If the image cannot be
  60. /// represented by GIF format, it will fallback to its real format which is determined by `original` data.
  61. public static let gif = FormatIndicatedCacheSerializer(imageFormat: .GIF)
  62. /// The indicated image format.
  63. private let imageFormat: ImageFormat
  64. /// Creates data which represents the given `image` under a format.
  65. public func data(with image: Image, original: Data?) -> Data? {
  66. func imageData(withFormat imageFormat: ImageFormat) -> Data? {
  67. switch imageFormat {
  68. case .PNG: return image.kf.pngRepresentation()
  69. case .JPEG: return image.kf.jpegRepresentation(compressionQuality: 1.0)
  70. case .GIF: return image.kf.gifRepresentation()
  71. case .unknown: return nil
  72. }
  73. }
  74. // generate data with indicated image format
  75. if let data = imageData(withFormat: imageFormat) {
  76. return data
  77. }
  78. let originalFormat = original?.kf.imageFormat ?? .unknown
  79. // generate data with original image's format
  80. if originalFormat != imageFormat, let data = imageData(withFormat: originalFormat) {
  81. return data
  82. }
  83. return original ?? image.kf.normalized.kf.pngRepresentation()
  84. }
  85. /// Same implementation as `DefaultCacheSerializer`.
  86. public func image(with data: Data, options: KingfisherParsedOptionsInfo) -> Image? {
  87. return KingfisherWrapper.image(data: data, options: options.imageCreatingOptions)
  88. }
  89. }