1

Getting a UIImage from a CIImgae via UIImage(ciImage:) seems to be a very old problem, which still seems to be an issue. Here is an old thread on stackoverflow which presents the same problem: old thread

I would have guessed that this problem should be fixed 6 major releases of iOS (I currently run the code for iOS 12.0), but it does not seem to be the case. Here is a sample playground code for you to try out and reproduce the problem:

import Foundation
import UIKit
import PlaygroundSupport

// extend UIImage to support cunstruction of solid color images
extension UIImage {
    public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1), contentScaleFactor: CGFloat = UIScreen.main.scale) {
        let rect = CGRect(origin: CGPoint.zero, size: size)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, contentScaleFactor)
        color.setFill()
        UIRectFill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        guard let cgImage = image?.cgImage else { return nil }
        self.init(cgImage: cgImage, scale: contentScaleFactor, orientation: .up)
    }
}

let uIImage = UIImage(color: UIColor.red, size: CGSize(width: 100.0, height: 100.0), contentScaleFactor: 1.0)!
let cIImage = CIImage(image: uIImage)!
let finalUIImage = UIImage(ciImage: cIImage)

let imageView = UIImageView(image: finalUIImage)
PlaygroundPage.current.liveView = imageView

Where the line let finalUIImage = UIImage(ciImage: cIImage) yields the error

Failed to convert UIImage to png

As stated in the answer to the previously mentioned old post on stackoverflow replacing let finalUIImage = UIImage(ciImage: cIImage) with

let context = CIContext()
let finalCGImage = context.createCGImage(cIImage, from: cIImage.extent)!
let finalUIImage = UIImage(cgImage: finalCGImage)

works.

I am posting this question because I want to know:

  • Can you reproduce this behavior?
  • Is this still a bug? (which is confusing as answer states that this should be fixed in iOS 6, but I encounter this problem in iOS 12)

(tested on XCode 10.1 beta 3 and XCode 10.0)

Wizard
  • 295
  • 1
  • 4
  • 15
  • I honestly can't say it's *still* an issue, but I've **always** chosen to use CoreGraphics to get my `UIImages`. Since I use a `GLKView` and already have my `CIContext` created, my subclassed version of it uses a simple property to get it. –  Oct 17 '18 at 13:45
  • 1
    Your code works fine in a playground for me and creates a 100x100 red view as expected. I'm pretty sure the "Failed to convert UIImage to png" is just the playground failing to create the quick-look preview of the image for the sidebar (because `image.pngData()` only works for images backed by a `CGImage`). – dan Oct 17 '18 at 17:25
  • @dan: Thanks. Now that I took a second look at it, it just seems to the quick-look preview. What brought me to this question though was something `CIFilter` related, where I wanted to get an `UIImage` from the `CIImage` filter output. And that worked with `createCGImage`, but not with `UIImage(ciImage:)`. I will take a second look there, too. – Wizard Oct 17 '18 at 20:25

0 Answers0