此代码示例在 macOS 游乐场中工作:
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")
升级到 Swift 4.1 后就没有了。错误:无法获得此 NSImage 的位图表示。
它现在如何在 Swift 4.1 中工作?
最佳答案
即使我使用的是 macOS 项目并且无法查明这里出了什么问题,我也遇到了同样的问题,但我找到了一种解决方法,可以修复 CIImage
的操场渲染问题。通过使用 CustomPlaygroundDisplayConvertible
只需将以下代码添加到您的操场顶部,您的图像将再次呈现
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)!
}
}
关于core-image - Swift 4.1 无法获得此 NSImage 的位图表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50722950/