You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.4 KiB

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