为什么这个受支持的CGBitmapContextCreate参数组合被拒绝为不受支持?

时间:2015-11-10 10:33:56

标签: swift core-graphics

我有以下Swift代码来制作CGBitmapContext

let imageDirectoryPath:String = "/Users/blah/blah/"
let imageFileName:String = "flower.tif"
let imageNS = NSImage(contentsOfFile: imageDirectoryPath + imageFileName)!
let imageCG = imageNS.CGImageForProposedRect(nil, context: nil, hints: nil)
var rawDataIn:[UInt8] = [UInt8](count: Int(imageNS.size.width) * Int(imageNS.size.height) * 4, repeatedValue: 0xff)
let context = CGBitmapContextCreate(
    &rawDataIn,
    Int(imageNS.size.width),
    Int(imageNS.size.height),
    8,
    Int(imageNS.size.width * 4),
    CGColorSpaceCreateDeviceRGB(),
    CGImageAlphaInfo.PremultipliedLast.rawValue)

我从这里得到一个错误,并使用Xcode的方案为项目设置环境变量CGBITMAP_CONTEXT_LOG_ERRORS我得到了关于错误的详细信息:

Nov 10 10:12:16  SwiftConsoleGrabcut[826] : 
    CGBitmapContextCreate: unsupported parameter combination:
        8 integer bits/component;
        32 bits/pixel;
        RGB color space model; kCGImageAlphaPremultipliedLast;
        19789 bytes/row.
    Valid parameters for RGB color space model are:
        16  bits per pixel,      5  bits per component,      kCGImageAlphaNoneSkipFirst
        32  bits per pixel,      8  bits per component,      kCGImageAlphaNoneSkipFirst
        32  bits per pixel,      8  bits per component,      kCGImageAlphaNoneSkipLast
        32  bits per pixel,      8  bits per component,      kCGImageAlphaPremultipliedFirst
        32  bits per pixel,      8  bits per component,      kCGImageAlphaPremultipliedLast
        64  bits per pixel,      16 bits per component,      kCGImageAlphaPremultipliedLast
        64  bits per pixel,      16 bits per component,      kCGImageAlphaNoneSkipLast
        128 bits per pixel,      32 bits per component,      kCGImageAlphaNoneSkipLast |kCGBitmapFloatComponents
        128 bits per pixel,      32 bits per component,      kCGImageAlphaPremultipliedLast |kCGBitmapFloatComponents
    See Quartz 2D Programming Guide (available online) for more information.

但我使用的参数组合,每像素32位,每个组件8位,kCGImageAlphaPremultipliedLast 列出了支持的组合之一。

为什么拒绝?

这可能是关于size.width的问题。我加载到imageNS的tif图像宽度为481像素,但imageNS.size.width报告为4947.4285714285716,这可以解释错误消息中给出的每行字节数的值。

0 个答案:

没有答案