ImageModifier.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // ImageModifier.swift
  3. // Kingfisher
  4. //
  5. // Created by Ethan Gill on 2017/11/28.
  6. //
  7. // Copyright (c) 2018 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. import Foundation
  27. /// An `ImageModifier` can be used to change properties on an Image in between
  28. /// cache serialization and use of the image.
  29. public protocol ImageModifier {
  30. /// Modify an input `Image`.
  31. ///
  32. /// - parameter image: Image which will be modified by `self`
  33. ///
  34. /// - returns: The modified image.
  35. ///
  36. /// - Note: The return value will be unmodified if modifying is not possible on
  37. /// the current platform.
  38. /// - Note: Most modifiers support UIImage or NSImage, but not CGImage.
  39. func modify(_ image: Image) -> Image
  40. }
  41. extension ImageModifier {
  42. func modify(_ image: Image?) -> Image? {
  43. guard let image = image else {
  44. return nil
  45. }
  46. return modify(image)
  47. }
  48. }
  49. typealias ModifierImp = ((Image) -> Image)
  50. fileprivate struct GeneralModifier: ImageModifier {
  51. let identifier: String
  52. let m: ModifierImp
  53. func modify(_ image: Image) -> Image {
  54. return m(image)
  55. }
  56. }
  57. /// The default modifier.
  58. /// Does nothing and returns the image it was given
  59. public struct DefaultImageModifier: ImageModifier {
  60. /// A default `DefaultImageModifier` which can be used everywhere.
  61. public static let `default` = DefaultImageModifier()
  62. /// Initialize a `DefaultImageModifier`
  63. private init() {}
  64. /// Modify an input `Image`.
  65. ///
  66. /// - parameter image: Image which will be modified by `self`
  67. ///
  68. /// - returns: The modified image.
  69. ///
  70. /// - Note: See documentation of `ImageModifier` protocol for more.
  71. public func modify(_ image: Image) -> Image {
  72. return image
  73. }
  74. }
  75. /// A custom modifier.
  76. /// Can be initialized with a block to modify images in a custom way
  77. public struct AnyImageModifier: ImageModifier {
  78. /// A block which modifies images, or returns the original image
  79. /// if modification cannot be performed.
  80. let block: (Image) -> Image
  81. /// Initialize an `AnyImageModifier`
  82. public init(modify: @escaping (Image) -> Image) {
  83. block = modify
  84. }
  85. /// Modifies an input `Image` using this `AnyImageModifier`'s `block`.
  86. ///
  87. /// - parameter image: Image which will be modified by `self`
  88. ///
  89. /// - returns: The modified image.
  90. ///
  91. /// - Note: See documentation of `ImageModifier` protocol for more.
  92. public func modify(_ image: Image) -> Image {
  93. return block(image)
  94. }
  95. }
  96. #if os(iOS) || os(tvOS) || os(watchOS)
  97. import UIKit
  98. /// Modifier for setting the rendering mode of images.
  99. /// Only UI-based images are supported; if a non-UI image is passed in, the
  100. /// modifier will do nothing.
  101. public struct RenderingModeImageModifier: ImageModifier {
  102. /// The rendering mode to apply to the image.
  103. public let renderingMode: UIImage.RenderingMode
  104. /// Initialize a `RenderingModeImageModifier`
  105. ///
  106. /// - parameter renderingMode: The rendering mode to apply to the image.
  107. /// Default is .automatic
  108. public init(renderingMode: UIImage.RenderingMode = .automatic) {
  109. self.renderingMode = renderingMode
  110. }
  111. /// Modify an input `Image`.
  112. ///
  113. /// - parameter image: Image which will be modified by `self`
  114. ///
  115. /// - returns: The modified image.
  116. ///
  117. /// - Note: See documentation of `ImageModifier` protocol for more.
  118. public func modify(_ image: Image) -> Image {
  119. return image.withRenderingMode(renderingMode)
  120. }
  121. }
  122. /// Modifier for setting the `flipsForRightToLeftLayoutDirection` property of images.
  123. /// Only UI-based images are supported; if a non-UI image is passed in, the
  124. /// modifier will do nothing.
  125. public struct FlipsForRightToLeftLayoutDirectionImageModifier: ImageModifier {
  126. /// Initialize a `FlipsForRightToLeftLayoutDirectionImageModifier`
  127. ///
  128. /// - Note: On versions of iOS lower than 9.0, the image will be returned
  129. /// unmodified.
  130. public init() {}
  131. /// Modify an input `Image`.
  132. ///
  133. /// - parameter image: Image which will be modified by `self`
  134. ///
  135. /// - returns: The modified image.
  136. ///
  137. /// - Note: See documentation of `ImageModifier` protocol for more.
  138. public func modify(_ image: Image) -> Image {
  139. if #available(iOS 9.0, *) {
  140. return image.imageFlippedForRightToLeftLayoutDirection()
  141. } else {
  142. return image
  143. }
  144. }
  145. }
  146. /// Modifier for setting the `alignmentRectInsets` property of images.
  147. /// Only UI-based images are supported; if a non-UI image is passed in, the
  148. /// modifier will do nothing.
  149. public struct AlignmentRectInsetsImageModifier: ImageModifier {
  150. /// The alignment insets to apply to the image
  151. public let alignmentInsets: UIEdgeInsets
  152. /// Initialize a `AlignmentRectInsetsImageModifier`
  153. public init(alignmentInsets: UIEdgeInsets) {
  154. self.alignmentInsets = alignmentInsets
  155. }
  156. /// Modify an input `Image`.
  157. ///
  158. /// - parameter image: Image which will be modified by `self`
  159. ///
  160. /// - returns: The modified image.
  161. ///
  162. /// - Note: See documentation of `ImageModifier` protocol for more.
  163. public func modify(_ image: Image) -> Image {
  164. return image.withAlignmentRectInsets(alignmentInsets)
  165. }
  166. }
  167. #endif