ImageModifier.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // ImageModifier.swift
  3. // Kingfisher
  4. //
  5. // Created by Ethan Gill on 2017/11/28.
  6. //
  7. // Copyright (c) 2019 Ethan Gill <ethan.gill@me.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. #if os(macOS)
  27. import AppKit
  28. #else
  29. import UIKit
  30. #endif
  31. /// An ``ImageModifier`` can be used to change properties on an image between cache serialization and the actual use of
  32. /// the image.
  33. ///
  34. /// The ``ImageModifier/modify(_:)`` method will be called after the image is retrieved from its source and before it
  35. /// is returned to the caller. This modified image is expected to be used only for rendering purposes; any changes
  36. /// applied by the ``ImageModifier`` will not be serialized or cached.
  37. public protocol ImageModifier: Sendable {
  38. /// Modify an input `Image`.
  39. ///
  40. /// - Parameter image: The image which will be modified by `self`.
  41. ///
  42. /// - Returns: The modified image.
  43. ///
  44. /// > Important: The return value will be unmodified if modification is not possible on the current platform.
  45. func modify(_ image: KFCrossPlatformImage) -> KFCrossPlatformImage
  46. }
  47. /// A wrapper that simplifies the creation of an ``ImageModifier``.
  48. ///
  49. /// This type conforms to ``ImageModifier`` and encapsulates an image modification block. If the `block` throws an
  50. /// error, the original image will be used.
  51. public struct AnyImageModifier: ImageModifier {
  52. /// A block that modifies images, or returns the original image if modification cannot be performed, along with an
  53. /// error.
  54. let block: @Sendable (KFCrossPlatformImage) throws -> KFCrossPlatformImage
  55. /// Creates an ``AnyImageModifier`` with a given `modify` block.
  56. /// - Parameter modify: A block which is used to modify the input image.
  57. public init(modify: @escaping @Sendable (KFCrossPlatformImage) throws -> KFCrossPlatformImage) {
  58. block = modify
  59. }
  60. public func modify(_ image: KFCrossPlatformImage) -> KFCrossPlatformImage {
  61. return (try? block(image)) ?? image
  62. }
  63. }
  64. #if os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
  65. import UIKit
  66. /// Modifier for setting the rendering mode of images.
  67. public struct RenderingModeImageModifier: ImageModifier {
  68. /// The rendering mode to apply to the image.
  69. public let renderingMode: UIImage.RenderingMode
  70. /// Creates a ``RenderingModeImageModifier``.
  71. ///
  72. /// - Parameter renderingMode: The rendering mode to apply to the image. The default is `.automatic`.
  73. public init(renderingMode: UIImage.RenderingMode = .automatic) {
  74. self.renderingMode = renderingMode
  75. }
  76. public func modify(_ image: KFCrossPlatformImage) -> KFCrossPlatformImage {
  77. return image.withRenderingMode(renderingMode)
  78. }
  79. }
  80. /// Modifier for setting the `flipsForRightToLeftLayoutDirection` property of images.
  81. public struct FlipsForRightToLeftLayoutDirectionImageModifier: ImageModifier {
  82. /// Creates a ``FlipsForRightToLeftLayoutDirectionImageModifier``.
  83. public init() {}
  84. public func modify(_ image: KFCrossPlatformImage) -> KFCrossPlatformImage {
  85. return image.imageFlippedForRightToLeftLayoutDirection()
  86. }
  87. }
  88. /// Modifier for setting the `alignmentRectInsets` property of images.
  89. public struct AlignmentRectInsetsImageModifier: ImageModifier {
  90. /// The alignment insets to apply to the image.
  91. public let alignmentInsets: UIEdgeInsets
  92. /// Creates a ``AlignmentRectInsetsImageModifier``.
  93. /// - Parameter alignmentInsets: The alignment insets to apply to the image.
  94. public init(alignmentInsets: UIEdgeInsets) {
  95. self.alignmentInsets = alignmentInsets
  96. }
  97. public func modify(_ image: KFCrossPlatformImage) -> KFCrossPlatformImage {
  98. return image.withAlignmentRectInsets(alignmentInsets)
  99. }
  100. }
  101. #endif