ImageModifier.swift 6.5 KB

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