ImageModifier.swift 4.8 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. The `modify(_:)` method will be called after the image retrieved from its source and before it returned
  33. /// to the caller. This modified image is expected to be only used for rendering purpose, any changes applied by the
  34. /// `ImageModifier` will not be serialized or cached.
  35. public protocol ImageModifier {
  36. /// Modify an input `Image`.
  37. ///
  38. /// - parameter image: Image which will be modified by `self`
  39. ///
  40. /// - returns: The modified image.
  41. ///
  42. /// - Note: The return value will be unmodified if modifying is not possible on
  43. /// the current platform.
  44. /// - Note: Most modifiers support UIImage or NSImage, but not CGImage.
  45. func modify(_ image: KFCrossPlatformImage) -> KFCrossPlatformImage
  46. }
  47. /// A wrapper for creating an `ImageModifier` easier.
  48. /// This type conforms to `ImageModifier` and wraps an image modify block.
  49. /// If the `block` throws an error, the original image will be used.
  50. public struct AnyImageModifier: ImageModifier {
  51. /// A block which modifies images, or returns the original image
  52. /// if modification cannot be performed with an error.
  53. let block: (KFCrossPlatformImage) throws -> KFCrossPlatformImage
  54. /// Creates an `AnyImageModifier` with a given `modify` block.
  55. public init(modify: @escaping (KFCrossPlatformImage) throws -> KFCrossPlatformImage) {
  56. block = modify
  57. }
  58. /// Modify an input `Image`. See `ImageModifier` protocol for more.
  59. public func modify(_ image: KFCrossPlatformImage) -> KFCrossPlatformImage {
  60. return (try? block(image)) ?? image
  61. }
  62. }
  63. #if os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
  64. import UIKit
  65. /// Modifier for setting the rendering mode of images.
  66. public struct RenderingModeImageModifier: ImageModifier {
  67. /// The rendering mode to apply to the image.
  68. public let renderingMode: UIImage.RenderingMode
  69. /// Creates a `RenderingModeImageModifier`.
  70. ///
  71. /// - Parameter renderingMode: The rendering mode to apply to the image. Default is `.automatic`.
  72. public init(renderingMode: UIImage.RenderingMode = .automatic) {
  73. self.renderingMode = renderingMode
  74. }
  75. /// Modify an input `Image`. See `ImageModifier` protocol for more.
  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. /// Modify an input `Image`. See `ImageModifier` protocol for more.
  85. public func modify(_ image: KFCrossPlatformImage) -> KFCrossPlatformImage {
  86. return image.imageFlippedForRightToLeftLayoutDirection()
  87. }
  88. }
  89. /// Modifier for setting the `alignmentRectInsets` property of images.
  90. public struct AlignmentRectInsetsImageModifier: ImageModifier {
  91. /// The alignment insets to apply to the image
  92. public let alignmentInsets: UIEdgeInsets
  93. /// Creates an `AlignmentRectInsetsImageModifier`.
  94. public init(alignmentInsets: UIEdgeInsets) {
  95. self.alignmentInsets = alignmentInsets
  96. }
  97. /// Modify an input `Image`. See `ImageModifier` protocol for more.
  98. public func modify(_ image: KFCrossPlatformImage) -> KFCrossPlatformImage {
  99. return image.withAlignmentRectInsets(alignmentInsets)
  100. }
  101. }
  102. #endif