我有一个用xcode 6.1创建的osx项目,我想用它来训练一下SWIFT的使用。
在我的一个视图中,我尝试创建一个NSBitMapImageRep,如下所示:
class BitmapView : NSView {
var image: NSBitmapImageRep!
override func awakeFromNib() {
var blub = NSBitmapImageRep(bitmapDataPlanes: nil,
pixelsWide: Int(self.frame.size.width),
pixelsHigh: Int(self.frame.size.height),
bitsPerSample: 8,
samplesPerPixel: 1,
hasAlpha: false,
isPlanar: false,
colorSpaceName: NSCalibratedRGBColorSpace,
bytesPerRow: 0, bitsPerPixel: 0)!
//test()
}}但是,每次我尝试运行它时,都会得到以下错误:
Inconsistent set of values to create NSBitmapImageRep fatal error: unexpectedly found nil while unwrapping an Optional value我想这是因为bitmapDataPlanes是零的。但它是一个可选的值,根据文档,允许为NULL。但是,传递NSNull()并不能编译。
有人能告诉我我要通过什么吗?o_O
发布于 2014-11-17 19:32:02
这个错误实际上是相当描述性的--您为初始化程序提供了一组不一致的值。具体来说,samplesPerPixel值1不能支持RGB颜色空间,这是您在colorSpaceName中指定的。From here
samplesPerPixel:每个像素的数据组件或样本的数量。此值包括颜色组件和覆盖组件(如果存在的话)。有意义的值范围从1到5。有青色、洋红色、黄色和黑色(CMYK)颜色成分的图像加上覆盖分量的spp值为5;没有覆盖分量的灰度图像的spp值为1。
因此,您只需将每个像素的样本更改为3:
var blub = NSBitmapImageRep(bitmapDataPlanes: nil,
pixelsWide: Int(100),
pixelsHigh: Int(100),
bitsPerSample: 8,
samplesPerPixel: 3,
hasAlpha: false,
isPlanar: false,
colorSpaceName: NSCalibratedRGBColorSpace,
bytesPerRow: 0, bitsPerPixel: 0)!https://stackoverflow.com/questions/26978985
复制相似问题