Skip to content

Commit

Permalink
Simplify the implementation of Image(uiImage:)
Browse files Browse the repository at this point in the history
  • Loading branch information
koher committed Nov 27, 2017
1 parent 8158c6f commit 75ea731
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 136 deletions.
133 changes: 13 additions & 120 deletions Sources/EasyImagy/ImageUIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ import UIKit
#if os(iOS) || os(tvOS)
import CoreImage
#endif

#if os(watchOS)
// Hack to simplify code
private enum CIImage {
var extent: CGRect { fatalError() }
}
private class CIContext {
func createCGImage(_ image: CIImage, from: CGRect) -> CGImage? { return nil }
}
extension UIImage {
fileprivate var ciImage: CIImage? { return nil }
}
#endif

extension Image where Pixel == RGBA<UInt8> {
public init(uiImage: UIImage) {
#if os(iOS) || os(tvOS)
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else if let ciImage = uiImage.ciImage {
Expand All @@ -23,17 +35,6 @@ extension Image where Pixel == RGBA<UInt8> {
}
self.init(width: 0, height: 0, pixels: [])
}
#else
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else {
// This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`.
guard uiImage.size == .zero else {
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though the `cgImage` of the instance is `nil`.")
}
self.init(width: 0, height: 0, pixels: [])
}
#endif
}

private init?(uiImageOrNil: UIImage?) {
Expand Down Expand Up @@ -66,7 +67,6 @@ extension Image where Pixel == RGBA<UInt8> {

extension Image where Pixel == RGBA<UInt16> {
public init(uiImage: UIImage) {
#if os(iOS) || os(tvOS)
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else if let ciImage = uiImage.ciImage {
Expand All @@ -83,17 +83,6 @@ extension Image where Pixel == RGBA<UInt16> {
}
self.init(width: 0, height: 0, pixels: [])
}
#else
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else {
// This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`.
guard uiImage.size == .zero else {
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though the `cgImage` of the instance is `nil`.")
}
self.init(width: 0, height: 0, pixels: [])
}
#endif
}

private init?(uiImageOrNil: UIImage?) {
Expand Down Expand Up @@ -126,7 +115,6 @@ extension Image where Pixel == RGBA<UInt16> {

extension Image where Pixel == RGBA<UInt32> {
public init(uiImage: UIImage) {
#if os(iOS) || os(tvOS)
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else if let ciImage = uiImage.ciImage {
Expand All @@ -143,17 +131,6 @@ extension Image where Pixel == RGBA<UInt32> {
}
self.init(width: 0, height: 0, pixels: [])
}
#else
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else {
// This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`.
guard uiImage.size == .zero else {
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though the `cgImage` of the instance is `nil`.")
}
self.init(width: 0, height: 0, pixels: [])
}
#endif
}

private init?(uiImageOrNil: UIImage?) {
Expand Down Expand Up @@ -186,7 +163,6 @@ extension Image where Pixel == RGBA<UInt32> {

extension Image where Pixel == RGBA<Float> {
public init(uiImage: UIImage) {
#if os(iOS) || os(tvOS)
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else if let ciImage = uiImage.ciImage {
Expand All @@ -203,17 +179,6 @@ extension Image where Pixel == RGBA<Float> {
}
self.init(width: 0, height: 0, pixels: [])
}
#else
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else {
// This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`.
guard uiImage.size == .zero else {
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though the `cgImage` of the instance is `nil`.")
}
self.init(width: 0, height: 0, pixels: [])
}
#endif
}

private init?(uiImageOrNil: UIImage?) {
Expand Down Expand Up @@ -246,7 +211,6 @@ extension Image where Pixel == RGBA<Float> {

extension Image where Pixel == RGBA<Double> {
public init(uiImage: UIImage) {
#if os(iOS) || os(tvOS)
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else if let ciImage = uiImage.ciImage {
Expand All @@ -263,17 +227,6 @@ extension Image where Pixel == RGBA<Double> {
}
self.init(width: 0, height: 0, pixels: [])
}
#else
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else {
// This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`.
guard uiImage.size == .zero else {
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though the `cgImage` of the instance is `nil`.")
}
self.init(width: 0, height: 0, pixels: [])
}
#endif
}

private init?(uiImageOrNil: UIImage?) {
Expand Down Expand Up @@ -306,7 +259,6 @@ extension Image where Pixel == RGBA<Double> {

extension Image where Pixel == UInt8 {
public init(uiImage: UIImage) {
#if os(iOS) || os(tvOS)
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else if let ciImage = uiImage.ciImage {
Expand All @@ -323,17 +275,6 @@ extension Image where Pixel == UInt8 {
}
self.init(width: 0, height: 0, pixels: [])
}
#else
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else {
// This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`.
guard uiImage.size == .zero else {
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though the `cgImage` of the instance is `nil`.")
}
self.init(width: 0, height: 0, pixels: [])
}
#endif
}

private init?(uiImageOrNil: UIImage?) {
Expand Down Expand Up @@ -366,7 +307,6 @@ extension Image where Pixel == UInt8 {

extension Image where Pixel == UInt16 {
public init(uiImage: UIImage) {
#if os(iOS) || os(tvOS)
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else if let ciImage = uiImage.ciImage {
Expand All @@ -383,17 +323,6 @@ extension Image where Pixel == UInt16 {
}
self.init(width: 0, height: 0, pixels: [])
}
#else
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else {
// This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`.
guard uiImage.size == .zero else {
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though the `cgImage` of the instance is `nil`.")
}
self.init(width: 0, height: 0, pixels: [])
}
#endif
}

private init?(uiImageOrNil: UIImage?) {
Expand Down Expand Up @@ -426,7 +355,6 @@ extension Image where Pixel == UInt16 {

extension Image where Pixel == UInt32 {
public init(uiImage: UIImage) {
#if os(iOS) || os(tvOS)
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else if let ciImage = uiImage.ciImage {
Expand All @@ -443,17 +371,6 @@ extension Image where Pixel == UInt32 {
}
self.init(width: 0, height: 0, pixels: [])
}
#else
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else {
// This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`.
guard uiImage.size == .zero else {
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though the `cgImage` of the instance is `nil`.")
}
self.init(width: 0, height: 0, pixels: [])
}
#endif
}

private init?(uiImageOrNil: UIImage?) {
Expand Down Expand Up @@ -486,7 +403,6 @@ extension Image where Pixel == UInt32 {

extension Image where Pixel == Float {
public init(uiImage: UIImage) {
#if os(iOS) || os(tvOS)
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else if let ciImage = uiImage.ciImage {
Expand All @@ -503,17 +419,6 @@ extension Image where Pixel == Float {
}
self.init(width: 0, height: 0, pixels: [])
}
#else
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else {
// This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`.
guard uiImage.size == .zero else {
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though the `cgImage` of the instance is `nil`.")
}
self.init(width: 0, height: 0, pixels: [])
}
#endif
}

private init?(uiImageOrNil: UIImage?) {
Expand Down Expand Up @@ -546,7 +451,6 @@ extension Image where Pixel == Float {

extension Image where Pixel == Double {
public init(uiImage: UIImage) {
#if os(iOS) || os(tvOS)
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else if let ciImage = uiImage.ciImage {
Expand All @@ -563,17 +467,6 @@ extension Image where Pixel == Double {
}
self.init(width: 0, height: 0, pixels: [])
}
#else
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
} else {
// This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`.
guard uiImage.size == .zero else {
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though the `cgImage` of the instance is `nil`.")
}
self.init(width: 0, height: 0, pixels: [])
}
#endif
}

private init?(uiImageOrNil: UIImage?) {
Expand Down
29 changes: 13 additions & 16 deletions Sources/EasyImagy/ImageUIKit.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,41 @@ import UIKit
#if os(iOS) || os(tvOS)
import CoreImage
#endif

#if os(watchOS)
// Hack to simplify code
private enum CIImage {
var extent: CGRect { fatalError() }
}
private class CIContext {
func createCGImage(_ image: CIImage, from: CGRect) -> CGImage? { return nil }
}
extension UIImage {
fileprivate var ciImage: CIImage? { return nil }
}
#endif
% for is_rgba in [True, False]:
% for type in types:
% pixel_type = f'RGBA<{type}>' if is_rgba else type

extension Image where Pixel == ${pixel_type} {
public init(uiImage: UIImage) {
% for core_image_available in [True, False]:
% if core_image_available:
#if os(iOS) || os(tvOS)
% else:
#else
% end
if let cgImage = uiImage.cgImage {
self.init(cgImage: cgImage)
% if core_image_available:
} else if let ciImage = uiImage.ciImage {
let context = CIContext()
// Fails when the `ciImage` has an infinite extent.
guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else {
fatalError("Failed to create a `CGImage` from an internal `CIImage` object from the given `UIImage` instance (\(uiImage)).")
}
self.init(cgImage: cgImage)
% end
} else {
// This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`.
guard uiImage.size == .zero else {
% if core_image_available:
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though both the `cgImage` and the `ciImage` of the instance are `nil`.")
% else:
fatalError("The `size` of the given `UIImage` instance (\(uiImage)) is not equal to `.zero` though the `cgImage` of the instance is `nil`.")
% end
}
self.init(width: 0, height: 0, pixels: [])
}
% if not core_image_available:
#endif
% end
% end
}

private init?(uiImageOrNil: UIImage?) {
Expand Down

0 comments on commit 75ea731

Please sign in to comment.