ImageModifier.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // ImageModifier.swift
  3. // Kingfisher
  4. //
  5. // Created by Ethan Gill on 2017/11/28.
  6. //
  7. // Copyright (c) 2017 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. /// Identifier of the modifier. It will be used to identify the modifier when
  31. /// modifying an image.
  32. ///
  33. /// - Note: Do not supply an empty string for a customized modifier, as the
  34. /// `DefaultImageModifier` uses this idenfifier. It is recommended to use a
  35. /// reverse domain name notation string of your own for the identifier.
  36. var identifier: String { get }
  37. /// Modify an input `Image`.
  38. ///
  39. /// - parameter image: Image which will be modified by `self`
  40. /// - parameter options: Options when modifying the image.
  41. ///
  42. /// - returns: The modified image.
  43. ///
  44. /// - Note: The return value will be unmodified if modifying is not possible on
  45. /// the current platform.
  46. /// - Note: Most modifiers support UIImage or NSImage, but not CGImage.
  47. func modify(image: Image, options: KingfisherOptionsInfo) -> Image?
  48. }
  49. typealias ModifierImp = ((Image, KingfisherOptionsInfo) -> Image?)
  50. public extension ImageModifier {
  51. /// Append an `ImageModifier` to another. The identifier of the new `ImageModifier`
  52. /// will be "\(self.identifier)|>\(another.identifier)".
  53. ///
  54. /// - parameter another: An `ImageModifier` you want to append to `self`.
  55. ///
  56. /// - returns: The new `ImageModifier` will process the image in the order
  57. /// of the two modifiers concatenated.
  58. public func append(another: ImageModifier) -> ImageModifier {
  59. let newIdentifier = identifier.appending("|>\(another.identifier)")
  60. return GeneralModifier(identifier: newIdentifier) {
  61. image, options in
  62. if let image = self.modify(image: image, options: options) {
  63. return another.modify(image: image, options: options)
  64. } else {
  65. return nil
  66. }
  67. }
  68. }
  69. }
  70. func ==(left: ImageModifier, right: ImageModifier) -> Bool {
  71. return left.identifier == right.identifier
  72. }
  73. func !=(left: ImageModifier, right: ImageModifier) -> Bool {
  74. return !(left == right)
  75. }
  76. fileprivate struct GeneralModifier: ImageModifier {
  77. let identifier: String
  78. let m: ModifierImp
  79. func modify(image: Image, options: KingfisherOptionsInfo) -> Image? {
  80. return m(image, options)
  81. }
  82. }
  83. /// The default modifier.
  84. /// Does nothing and returns the image it was given
  85. public struct DefaultImageModifier: ImageModifier {
  86. /// A default `DefaultImageModifier` which can be used everywhere.
  87. public static let `default` = DefaultImageModifier()
  88. /// Identifier of the modifier.
  89. /// - Note: See documentation of `ImageModifier` protocol for more.
  90. public let identifier = ""
  91. /// Initialize a `DefaultImageModifier`
  92. public init() {}
  93. /// Modify an input `Image`.
  94. ///
  95. /// - parameter image: Image which will be modified by `self`
  96. /// - parameter options: Options when modifying the image.
  97. ///
  98. /// - returns: The modified image.
  99. ///
  100. /// - Note: See documentation of `ImageModifier` protocol for more.
  101. public func modify(image: Image, options: KingfisherOptionsInfo) -> Image? {
  102. return image
  103. }
  104. }
  105. #if os(iOS) || os(tvOS) || os(watchOS)
  106. import UIKit
  107. /// Modifier for setting the rendering mode of images.
  108. /// Only UI-based images are supported; if a non-UI image is passed in, the modifier
  109. /// will do nothing.
  110. public struct RenderingModeImageModifier: ImageModifier {
  111. /// Identifier of the modifier.
  112. /// - Note: See documentation of `ImageModifier` protocol for more.
  113. public let identifier: String
  114. /// The rendering mode to apply to the image.
  115. public let renderingMode: UIImageRenderingMode
  116. /// Initialize a `RenderingModeImageModifier`
  117. ///
  118. /// - parameter renderingMode: The rendering mode to apply to the image.
  119. /// Default is .automatic
  120. public init(renderingMode: UIImageRenderingMode = .automatic) {
  121. self.renderingMode = renderingMode
  122. self.identifier = "com.onevcat.Kingfisher.RenderingModeImageModifier(\(renderingMode))"
  123. }
  124. /// Modify an input `Image`.
  125. ///
  126. /// - parameter image: Image which will be modified by `self`
  127. /// - parameter options: Options when modifying the image.
  128. ///
  129. /// - returns: The modified image.
  130. ///
  131. /// - Note: See documentation of `ImageModifier` protocol for more.
  132. public func modify(image: Image, options: KingfisherOptionsInfo) -> Image? {
  133. return image.withRenderingMode(renderingMode)
  134. }
  135. }
  136. public struct FlipsForRightToLeftLayoutDirectionImageModifier: ImageModifier {
  137. /// Identifier of the modifier.
  138. /// - Note: See documentation of `ImageModifier` protocol for more.
  139. public let identifier: String
  140. /// Initialize a `FlipsForRightToLeftLayoutDirectionImageModifier`
  141. ///
  142. /// - Note: On versions of iOS lower than 9.0, the image will be returned
  143. /// unmodified.
  144. public init() {
  145. self.identifier = "com.onevcat.Kingfisher.FlipsForRightToLeftLayoutDirectionImageModifier"
  146. }
  147. /// Modify an input `Image`.
  148. ///
  149. /// - parameter image: Image which will be modified by `self`
  150. /// - parameter options: Options when modifying the image.
  151. ///
  152. /// - returns: The modified image.
  153. ///
  154. /// - Note: See documentation of `ImageModifier` protocol for more.
  155. public func modify(image: Image, options: KingfisherOptionsInfo) -> Image? {
  156. if #available(iOS 9.0, *) {
  157. return image.imageFlippedForRightToLeftLayoutDirection()
  158. } else {
  159. return image
  160. }
  161. }
  162. }
  163. #endif