7

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

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

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

context = CGBitmapContextCreate(NULL,
                                (int)pi.bufferSizeRequired.width,
                                (int)pi.bufferSizeRequired.height,
                                CGImageGetBitsPerComponent(imageRef),
                                0,
                                CGImageGetColorSpace(imageRef),
                                CGImageGetBitmapInfo(imageRef));

宽度为 2626,高度为 3981。我将 bytesPerRow 设为零,以便为我自动计算它,并且它自动选择了 7936。

那么,到底哪里不一致呢?它快把我逼疯了。

4

5 回答 5

20

由于我不明白的原因,我通过将 BitmapInfo 参数设置为kCGImageAlphaNoneSkipLast.

于 2012-11-26T08:45:19.140 回答
6

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

Quartz 2D Programming 文档中列出了支持的像素格式。不支持 8/3/24 组合,但 8/3/32 支持,与是否使用 alpha 无关。

于 2012-11-23T14:30:54.217 回答
3

海因里希给了你一个很好的答案背景。只是想我会提供我的具体案例,作为tarmes答案的替代方案。该答案的问题在于,如果您想要存在 Alpha 通道,它并不能解决问题。当我遇到这个问题时,我正在使用Trevor Harmon 的 UIImage+Alpha类别。在代码中我发现了这条评论:

// The bitsPerComponent and bitmapInfo values are hard-coded to prevent an "unsupported parameter combination" error

现在这个硬编码的修复是在调用的方法之一中CGBitmapContextCreate,但不是在它后面的方法中。所以对我来说,这只是按照作者自己的建议用他的其他方法之一解决问题的问题;)

显然,某些部分CGBitmapInfo没有从相关图像中正确传递,尽管我不知道为什么。

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

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

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

于 2014-08-31T08:24:48.413 回答
1

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

我的问题是:

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位/像素;1-分量色彩空间;kCGImageAlphaNone; 2147483648 字节/行。

然后我发现一个错误是header_fontsize没有分配任何值(因为我把fontsize和header_fontsize弄错了)。错误就在这里:dimensions:CGSizeMake(header_fontsize*9, txt_h)header_fontsize 未分配任何值(它不是 0,分配header_fontsize = 0仍然可以);重新分配一个值来header_fontsize解决问题。

希望这会对类似情况的人有所帮助,例如 Sprite 案例。

于 2014-09-22T17:15:48.320 回答
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
    }
  }
}

有关更多信息(以及支持的像素格式列表),请参阅https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html

于 2021-06-07T11:03:16.263 回答