Avatar
#beginner-help-archived のやつ、 #ifelse if 消すことができなかったから、コードの重複を消すために gyb で制御して #if を生成するという闇コードに・・・。 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() // This `guard` can be replaced with `!` if you are sure that the `createCGImage` below never fails. guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { fatalError("Failed to create a `CGImage` from an internal `CIImage` object from this `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 this `UIImage` instance (\(uiImage)) is not equal to `.zero` though both `cgImage` and `ciImage` of the instance are `nil`.") % else: fatalError("The `size` of this `UIImage` instance (\(uiImage)) is not equal to `.zero` though `cgImage` of the instance is `nil`.") % end } self.init(width: 0, height: 0, pixels: []) } % if not core_image_available: #endif % end % end }