Macos CGBitmapContextCreate:不支持的参数组合

Macos CGBitmapContextCreate:不支持的参数组合,macos,core-graphics,Macos,Core Graphics,创建位图上下文时出现以下错误: CGBitmapContextCreate:不支持的参数组合:8个整数位/分量;24位/像素;三分量颜色空间;KCGIMAGEALPHONE;7936字节/行 下面是代码(请注意,上下文基于现有CGImage的参数: context = CGBitmapContextCreate(NULL, (int)pi.bufferSizeRequired.width,

创建位图上下文时出现以下错误:

CGBitmapContextCreate:不支持的参数组合:8个整数位/分量;24位/像素;三分量颜色空间;KCGIMAGEALPHONE;7936字节/行

下面是代码(请注意,上下文基于现有CGImage的参数:

context = CGBitmapContextCreate(NULL,
                                (int)pi.bufferSizeRequired.width,
                                (int)pi.bufferSizeRequired.height,
                                CGImageGetBitsPerComponent(imageRef),
                                0,
                                CGImageGetColorSpace(imageRef),
                                CGImageGetBitmapInfo(imageRef));
宽度是2626,高度是3981。我把bytesPerRow的值设为零,这样我就可以自动计算出它,它自动选择了7936

那么,矛盾到底在哪里?这让我发疯

CGBitmapContextCreate:不支持的参数组合:8整数位/组件;24位/像素;3组件颜色空间;kCGImageAlphaNone;7936字节/行


Quartz 2D编程文档中列出了。8/3/24组合不受支持,但8/3/32独立于是否使用alpha。

出于我不理解的原因,我通过将BitmapInfo参数设置为
KCGIMAGEALPHANEONSKIPLAST
解决了这个问题。Heinrich为您提供了一个很好的答案背景。Just认为我应该提供我的具体案例,作为tarmes答案的替代方案。该答案的问题是,如果您希望alpha通道存在,它无法解决问题。遇到此问题时,我使用了一个名为的类别。在代码中,我发现了以下注释:

// The bitsPerComponent and bitmapInfo values are hard-coded to prevent an "unsupported parameter combination" error
现在,这个硬编码的修复是在调用
CGBitmapContextCreate
的方法之一中,而不是在它后面的方法中。因此,对我来说,只需遵循作者自己的建议,在他的另一个方法中修复问题;)

显然,
CGBitmapInfo
的某些部分没有从所讨论的图像中正确传递,尽管我不知道为什么

因此,如果使用alpha通道,请在bitmapInfo中使用这些常量:
kCGBitmapByteOrderDefault | KCGimageAlphaPremultipledFirst

否则,我只想指出,如果您处理别名问题,它是一个非常有用的类


(另外,值得一提的是,这个问题只在Xcode 6中出现过……)

我不确定它是否对任何人都有帮助,我只是遇到了类似的问题,并按照建议进行了尝试,但没有运气

我的问题是:

CCLabelTTF *header_txt = [CCLabelTTF 
   labelWithString:header 
   fontName:fontname fontSize:header_fontsize 
   dimensions:CGSizeMake(header_fontsize*9, txt_h) 
   hAlignment:kCCTextAlignmentLeft 
   vAlignment:kCCVerticalTextAlignmentCenter];
有误:

:CGBitmapContextCreate:不支持的参数组合:8个整数位/组件;8位/像素;单分量颜色空间;KCGIMAGEALPHONE;2147483648字节/行

然后我发现一个错误是header_fontsize没有分配任何值(因为我将fontsize与header_fontsize弄错了)。错误在这里:
dimensions:CGSizeMake(header_fontsize*9,txt_h)
未分配header_fontsize的任何值(不是0,分配
header_fontsize=0
仍然可以);将值重新分配给
标题大小
修复了该问题


希望这将有助于类似情况下的人,如雪碧的情况

不支持某些像素格式

您可以提前检查是否支持任何图像:

extension CGImage {
  public var hasCGContextSupportedPixelFormat: Bool {
    guard let colorSpace = self.colorSpace else {
      return false
    }
    #if os(iOS) || os(watchOS) || os(tvOS)
    let iOS = true
    #else
    let iOS = false
    #endif

    #if os(OSX)
    let macOS = true
    #else
    let macOS = false
    #endif
    switch (colorSpace.model, bitsPerPixel, bitsPerComponent, alphaInfo, bitmapInfo.contains(.floatComponents)) {
    case (.unknown, 8, 8, .alphaOnly, _):
      return macOS || iOS
    case (.monochrome, 8, 8, .none, _):
      return macOS || iOS
    case (.monochrome, 8, 8, .alphaOnly, _):
      return macOS || iOS
    case (.monochrome, 16, 16, .none, _):
      return macOS
    case (.monochrome, 32, 32, .none, true):
      return macOS
    case (.rgb, 16, 5, .noneSkipFirst, _):
      return macOS || iOS
    case (.rgb, 32, 8, .noneSkipFirst, _):
      return macOS || iOS
    case (.rgb, 32, 8, .noneSkipLast, _):
      return macOS || iOS
    case (.rgb, 32, 8, .premultipliedFirst, _):
      return macOS || iOS
    case (.rgb, 32, 8, .premultipliedLast, _):
      return macOS || iOS
    case (.rgb, 64, 16, .premultipliedLast, _):
      return macOS
    case (.rgb, 64, 16, .noneSkipLast, _):
      return macOS
    case (.rgb, 128, 32, .noneSkipLast, true):
      return macOS
    case (.rgb, 128, 32, .premultipliedLast, true):
      return macOS
    case (.cmyk, 32, 8, .none, _):
      return macOS
    case (.cmyk, 64, 16, .none, _):
      return macOS
    case (.cmyk, 128, 32, .none, true):
      return macOS
    default:
      return false
    }
  }
}

有关更多信息(以及支持的像素格式列表),请参阅。

谢谢!你太棒了。这解决了一个非常令人困惑的问题。我使用ZBar SDK读取条形码时出现此错误。使用
kgimageAlphaNoneSkiplast
更改
CGImageGetBitmapInfo(imageRef)
似乎有效。谢谢。您的“bytesPerRow”=0应该是每像素4个通道,即numPixels/行,而不是0。将其更改为宽度*4公式(2626*4),它应该可以工作。:)支持的像素格式链接的向上投票。发现这一点让一切变得容易多了。