This code sample was working in a macOS playground:
import Cocoa
import XCPlayground
func getResImg(name: String, ext: String) -> CIImage {
guard let fileURL = Bundle.main.url(forResource: name, withExtension: ext) else {
fatalError("can't find image")
}
guard let img = CIImage(contentsOf: fileURL) else {
fatalError("can't load image")
}
return img
}
var img = getResImg(name: "noise", ext: "jpg")
After upgrading to Swift 4.1 it doesn't. Error: Failed to get a bitmap representation of this NSImage.
How does it work now in Swift 4.1?
I ran into the same issue even though I was using a macOS project and wasn't able to pinpoint what's going wrong here, but I found a workaround which fixes playground rendering for CIImage
by using a CustomPlaygroundDisplayConvertible
Just add the following code to the top of your playground and your images will render again
extension CIImage: CustomPlaygroundDisplayConvertible {
static let playgroundRenderContext = CIContext()
public var playgroundDescription: Any {
let jpgData = CIImage.playgroundRenderContext.jpegRepresentation(of: self, colorSpace: CGColorSpace(name: CGColorSpace.sRGB)!, options: [:])!
return NSImage(data: jpgData)!
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With