ImageModifier.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /// The default modifier.
  52. /// It does nothing and returns the image as is.
  53. public struct DefaultImageModifier: ImageModifier {
  54. /// A default `DefaultImageModifier` which can be used everywhere.
  55. public static let `default` = DefaultImageModifier()
  56. private init() {}
  57. /// Modifie an input `Image`. See `ImageModifier` protocol for more.
  58. public func modify(_ image: Image) -> Image { return image }
  59. }
  60. /// A wrapper for creating an `ImageModifier` easier.
  61. /// This type conforms to `ImageModifier` and wraps an image modify block.
  62. /// If the `block` throws an error, the original image will be used.
  63. public struct AnyImageModifier: ImageModifier {
  64. /// A block which modifies images, or returns the original image
  65. /// if modification cannot be performed with an error.
  66. let block: (Image) throws -> Image
  67. /// Creates an `AnyImageModifier` with a given `modify` block.
  68. public init(modify: @escaping (Image) throws -> Image) {
  69. block = modify
  70. }
  71. /// Modify an input `Image`. See `ImageModifier` protocol for more.
  72. public func modify(_ image: Image) -> Image {
  73. return (try? block(image)) ?? image
  74. }
  75. }
  76. #if os(iOS) || os(tvOS) || os(watchOS)
  77. import UIKit
  78. /// Modifier for setting the rendering mode of images.
  79. public struct RenderingModeImageModifier: ImageModifier {
  80. /// The rendering mode to apply to the image.
  81. public let renderingMode: UIImage.RenderingMode
  82. /// Creates a `RenderingModeImageModifier`.
  83. ///
  84. /// - Parameter renderingMode: The rendering mode to apply to the image. Default is `.automatic`.
  85. public init(renderingMode: UIImage.RenderingMode = .automatic) {
  86. self.renderingMode = renderingMode
  87. }
  88. /// Modify an input `Image`. See `ImageModifier` protocol for more.
  89. public func modify(_ image: Image) -> Image {
  90. return image.withRenderingMode(renderingMode)
  91. }
  92. }
  93. /// Modifier for setting the `flipsForRightToLeftLayoutDirection` property of images.
  94. public struct FlipsForRightToLeftLayoutDirectionImageModifier: ImageModifier {
  95. /// Creates a `FlipsForRightToLeftLayoutDirectionImageModifier`.
  96. public init() {}
  97. /// Modify an input `Image`. See `ImageModifier` protocol for more.
  98. public func modify(_ image: Image) -> Image {
  99. return image.imageFlippedForRightToLeftLayoutDirection()
  100. }
  101. }
  102. /// Modifier for setting the `alignmentRectInsets` property of images.
  103. public struct AlignmentRectInsetsImageModifier: ImageModifier {
  104. /// The alignment insets to apply to the image
  105. public let alignmentInsets: UIEdgeInsets
  106. /// Creates an `AlignmentRectInsetsImageModifier`.
  107. public init(alignmentInsets: UIEdgeInsets) {
  108. self.alignmentInsets = alignmentInsets
  109. }
  110. /// Modify an input `Image`. See `ImageModifier` protocol for more.
  111. public func modify(_ image: Image) -> Image {
  112. return image.withAlignmentRectInsets(alignmentInsets)
  113. }
  114. }
  115. #endif