|
|
@@ -38,7 +38,16 @@ public protocol ImageModifier {
|
|
|
/// - Note: The return value will be unmodified if modifying is not possible on
|
|
|
/// the current platform.
|
|
|
/// - Note: Most modifiers support UIImage or NSImage, but not CGImage.
|
|
|
- func modify(image: Image) -> Image
|
|
|
+ func modify(_ image: Image) -> Image
|
|
|
+}
|
|
|
+
|
|
|
+extension ImageModifier {
|
|
|
+ func modify(_ image: Image?) -> Image? {
|
|
|
+ guard let image = image else {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return modify(image)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
typealias ModifierImp = ((Image) -> Image)
|
|
|
@@ -46,7 +55,7 @@ typealias ModifierImp = ((Image) -> Image)
|
|
|
fileprivate struct GeneralModifier: ImageModifier {
|
|
|
let identifier: String
|
|
|
let m: ModifierImp
|
|
|
- func modify(image: Image) -> Image {
|
|
|
+ func modify(_ image: Image) -> Image {
|
|
|
return m(image)
|
|
|
}
|
|
|
}
|
|
|
@@ -68,7 +77,7 @@ public struct DefaultImageModifier: ImageModifier {
|
|
|
/// - returns: The modified image.
|
|
|
///
|
|
|
/// - Note: See documentation of `ImageModifier` protocol for more.
|
|
|
- public func modify(image: Image) -> Image {
|
|
|
+ public func modify(_ image: Image) -> Image {
|
|
|
return image
|
|
|
}
|
|
|
}
|
|
|
@@ -93,7 +102,7 @@ public struct AnyImageModifier: ImageModifier {
|
|
|
/// - returns: The modified image.
|
|
|
///
|
|
|
/// - Note: See documentation of `ImageModifier` protocol for more.
|
|
|
- public func modify(image: Image) -> Image {
|
|
|
+ public func modify(_ image: Image) -> Image {
|
|
|
return block(image)
|
|
|
}
|
|
|
}
|
|
|
@@ -101,83 +110,82 @@ public struct AnyImageModifier: ImageModifier {
|
|
|
#if os(iOS) || os(tvOS) || os(watchOS)
|
|
|
import UIKit
|
|
|
|
|
|
- /// Modifier for setting the rendering mode of images.
|
|
|
- /// Only UI-based images are supported; if a non-UI image is passed in, the
|
|
|
- /// modifier will do nothing.
|
|
|
- public struct RenderingModeImageModifier: ImageModifier {
|
|
|
+/// Modifier for setting the rendering mode of images.
|
|
|
+/// Only UI-based images are supported; if a non-UI image is passed in, the
|
|
|
+/// modifier will do nothing.
|
|
|
+public struct RenderingModeImageModifier: ImageModifier {
|
|
|
|
|
|
- /// The rendering mode to apply to the image.
|
|
|
- public let renderingMode: UIImageRenderingMode
|
|
|
+ /// The rendering mode to apply to the image.
|
|
|
+ public let renderingMode: UIImageRenderingMode
|
|
|
|
|
|
- /// Initialize a `RenderingModeImageModifier`
|
|
|
- ///
|
|
|
- /// - parameter renderingMode: The rendering mode to apply to the image.
|
|
|
- /// Default is .automatic
|
|
|
- public init(renderingMode: UIImageRenderingMode = .automatic) {
|
|
|
- self.renderingMode = renderingMode
|
|
|
- }
|
|
|
+ /// Initialize a `RenderingModeImageModifier`
|
|
|
+ ///
|
|
|
+ /// - parameter renderingMode: The rendering mode to apply to the image.
|
|
|
+ /// Default is .automatic
|
|
|
+ public init(renderingMode: UIImageRenderingMode = .automatic) {
|
|
|
+ self.renderingMode = renderingMode
|
|
|
+ }
|
|
|
|
|
|
- /// Modify an input `Image`.
|
|
|
- ///
|
|
|
- /// - parameter image: Image which will be modified by `self`
|
|
|
- ///
|
|
|
- /// - returns: The modified image.
|
|
|
- ///
|
|
|
- /// - Note: See documentation of `ImageModifier` protocol for more.
|
|
|
- public func modify(image: Image) -> Image {
|
|
|
- return image.withRenderingMode(renderingMode)
|
|
|
- }
|
|
|
+ /// Modify an input `Image`.
|
|
|
+ ///
|
|
|
+ /// - parameter image: Image which will be modified by `self`
|
|
|
+ ///
|
|
|
+ /// - returns: The modified image.
|
|
|
+ ///
|
|
|
+ /// - Note: See documentation of `ImageModifier` protocol for more.
|
|
|
+ public func modify(_ image: Image) -> Image {
|
|
|
+ return image.withRenderingMode(renderingMode)
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+/// Modifier for setting the `flipsForRightToLeftLayoutDirection` property of images.
|
|
|
+/// Only UI-based images are supported; if a non-UI image is passed in, the
|
|
|
+/// modifier will do nothing.
|
|
|
+public struct FlipsForRightToLeftLayoutDirectionImageModifier: ImageModifier {
|
|
|
+ /// Initialize a `FlipsForRightToLeftLayoutDirectionImageModifier`
|
|
|
+ ///
|
|
|
+ /// - Note: On versions of iOS lower than 9.0, the image will be returned
|
|
|
+ /// unmodified.
|
|
|
+ public init() {}
|
|
|
|
|
|
- /// Modifier for setting the `flipsForRightToLeftLayoutDirection` property of images.
|
|
|
- /// Only UI-based images are supported; if a non-UI image is passed in, the
|
|
|
- /// modifier will do nothing.
|
|
|
- public struct FlipsForRightToLeftLayoutDirectionImageModifier: ImageModifier {
|
|
|
- /// Initialize a `FlipsForRightToLeftLayoutDirectionImageModifier`
|
|
|
- ///
|
|
|
- /// - Note: On versions of iOS lower than 9.0, the image will be returned
|
|
|
- /// unmodified.
|
|
|
- public init() {}
|
|
|
-
|
|
|
- /// Modify an input `Image`.
|
|
|
- ///
|
|
|
- /// - parameter image: Image which will be modified by `self`
|
|
|
- ///
|
|
|
- /// - returns: The modified image.
|
|
|
- ///
|
|
|
- /// - Note: See documentation of `ImageModifier` protocol for more.
|
|
|
- public func modify(image: Image) -> Image {
|
|
|
- if #available(iOS 9.0, *) {
|
|
|
- return image.imageFlippedForRightToLeftLayoutDirection()
|
|
|
- } else {
|
|
|
- return image
|
|
|
- }
|
|
|
+ /// Modify an input `Image`.
|
|
|
+ ///
|
|
|
+ /// - parameter image: Image which will be modified by `self`
|
|
|
+ ///
|
|
|
+ /// - returns: The modified image.
|
|
|
+ ///
|
|
|
+ /// - Note: See documentation of `ImageModifier` protocol for more.
|
|
|
+ public func modify(_ image: Image) -> Image {
|
|
|
+ if #available(iOS 9.0, *) {
|
|
|
+ return image.imageFlippedForRightToLeftLayoutDirection()
|
|
|
+ } else {
|
|
|
+ return image
|
|
|
}
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- /// Modifier for setting the `alignmentRectInsets` property of images.
|
|
|
- /// Only UI-based images are supported; if a non-UI image is passed in, the
|
|
|
- /// modifier will do nothing.
|
|
|
- public struct AlignmentRectInsetsImageModifier: ImageModifier {
|
|
|
+/// Modifier for setting the `alignmentRectInsets` property of images.
|
|
|
+/// Only UI-based images are supported; if a non-UI image is passed in, the
|
|
|
+/// modifier will do nothing.
|
|
|
+public struct AlignmentRectInsetsImageModifier: ImageModifier {
|
|
|
|
|
|
- /// The alignment insets to apply to the image
|
|
|
- public let alignmentInsets: UIEdgeInsets
|
|
|
+ /// The alignment insets to apply to the image
|
|
|
+ public let alignmentInsets: UIEdgeInsets
|
|
|
|
|
|
- /// Initialize a `AlignmentRectInsetsImageModifier`
|
|
|
- public init(alignmentInsets: UIEdgeInsets) {
|
|
|
- self.alignmentInsets = alignmentInsets
|
|
|
- }
|
|
|
-
|
|
|
- /// Modify an input `Image`.
|
|
|
- ///
|
|
|
- /// - parameter image: Image which will be modified by `self`
|
|
|
- ///
|
|
|
- /// - returns: The modified image.
|
|
|
- ///
|
|
|
- /// - Note: See documentation of `ImageModifier` protocol for more.
|
|
|
- public func modify(image: Image) -> Image {
|
|
|
- return image.withAlignmentRectInsets(alignmentInsets)
|
|
|
- }
|
|
|
+ /// Initialize a `AlignmentRectInsetsImageModifier`
|
|
|
+ public init(alignmentInsets: UIEdgeInsets) {
|
|
|
+ self.alignmentInsets = alignmentInsets
|
|
|
}
|
|
|
|
|
|
+ /// Modify an input `Image`.
|
|
|
+ ///
|
|
|
+ /// - parameter image: Image which will be modified by `self`
|
|
|
+ ///
|
|
|
+ /// - returns: The modified image.
|
|
|
+ ///
|
|
|
+ /// - Note: See documentation of `ImageModifier` protocol for more.
|
|
|
+ public func modify(_ image: Image) -> Image {
|
|
|
+ return image.withAlignmentRectInsets(alignmentInsets)
|
|
|
+ }
|
|
|
+}
|
|
|
#endif
|