【问题标题】:CGBitmapContextCreate: unsupported parameter combinationCGBitmapContextCreate:不支持的参数组合
【发布时间】:2012-11-23 10:58:29
【问题描述】:

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

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 设置为 0,以便为我自动计算它,它会自动选择 7936。

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

【问题讨论】:

    标签: macos core-graphics


    【解决方案1】:

    由于我不明白的原因,我通过将 BitmapInfo 参数设置为 kCGImageAlphaNoneSkipLast 解决了这个问题。

    【讨论】:

    • 谢谢!你太棒了。这解决了一个非常令人困惑的问题。
    • 是的。我使用 ZBar SDK 读取条形码时收到此错误。用kCGImageAlphaNoneSkipLast 更改CGImageGetBitmapInfo(imageRef) 似乎有效。谢谢。
    【解决方案2】:

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

    Quartz 2D Programming 文档中列出了supported pixel formats。不支持 8/3/24 组合,但 8/3/32 支持,与是否使用 alpha 无关。

    【讨论】:

    • 您的“bytesPerRow”= 0 应该是每个像素 4 个通道,即 numPixels/row,而不是 0。将其更改为宽度 * 4 公式 (2626*4),它应该可以工作。 :)
    • 对支持的像素格式的链接进行投票。发现这一点让一切变得更容易。
    【解决方案3】:

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

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

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

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

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

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

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

    【讨论】:

      【解决方案4】:

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

      我的问题是:

      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) with header_fontsize unassigned any value(它不是0,分配header_fontsize = 0still ok);将值重新分配给 header_fontsize 解决了这个问题。

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

      【讨论】:

        【解决方案5】:

        只是不支持某些像素格式。

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

        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 了解更多信息(以及支持的像素格式列表)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-09
          • 1970-01-01
          相关资源
          最近更新 更多