【问题标题】:I am getting unsupported parameter combination CGBitmap error with swift我得到不受支持的参数组合 CGBitmap 错误与 swift
【发布时间】:2014-06-08 18:00:40
【问题描述】:

我正在尝试快速创建一个 CGContext。它编译但在运行时抛出错误。

let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let context:CGContext = CGBitmapContextCreate(nil, 20, 20, 8, 0, colorSpace, CGBitmapInfo.AlphaInfoMask)
CGColorSpaceRelease(colorSpace);

....

错误是:

Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; unrecognized; 96 bytes/row.
fatal error: Can't unwrap Optional.None

【问题讨论】:

    标签: ios swift xcode cgcontext cgbitmapcontext


    【解决方案1】:

    以防万一有人遇到同样的问题。下面的 sn-p 终于可以用了。

    let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
    let context = CGBitmapContextCreate(nil, UInt(rect.size.width), UInt(rect.size.height), 8, 0, colorSpace, bitmapInfo)
    

    它在 swift 中生成一个 32 位 RGBA 上下文

    【讨论】:

    • 使用当前版本的 Swift(我使用的是 Xcode 6.1):let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
    • 现在Xcode的当前版本是: let context = CGBitmapContextCreate(nil, Int(rect.size.width), Int(rect.size.height), 8, 0, colorSpace,位图信息)
    • Swift 2.0 实际上需要 UInt32 而不是 CGBitMapInfo 对象,因此在 Swift 2.0 中使用 let context = CGBitmapContextCreate(nil, Int(rect.size.width), Int(rect.size.height), 8, 0, colorSpace, bitmapInfo.rawValue)。 rawValue 将 CGBitMapInfo 转换为 UInt32
    • Swift 2 让 context = CGBitmapContextCreate(nil, width, height, bitsPerComponent, bytesPerRow, colourSpace, CGBitmapInfo.ByteOrder32Little.rawValue)
    • 我想使用其他 PremultipliedLast 但如果我没有使用,第一个或最后一个上下文变为 nil
    【解决方案2】:

    为 Swift 3 更新:

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
        guard let context = CGContext.init(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: Int(bitsPerComponent), bytesPerRow: Int(bytesPerRow), space: colorSpace, bitmapInfo: UInt32(bitmapInfo.rawValue)) else {
            // cannot create context - handle error
        }
    

    【讨论】:

      【解决方案3】:

      在 Swift 2.1 中,人们可以正确访问这些字段,甚至可以将它们组合在一起:

      let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue)
      
      let context = CGBitmapContextCreate(baseAddress, width, height, 8,
                      bytesPerRow, colorSpace, bitmapInfo.rawValue);
      

      大量的“rawValue”正在发生:)

      你甚至不需要分离出bitmapInfo,并且可以做一个单行:

      let context = CGBitmapContextCreate(baseAddress, width, height, 8,
                      bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue
      

      【讨论】:

        【解决方案4】:

        Swift 5 更新:

         let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
         let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
         let context = CGContext(data: nil, width: UInt(rect.size.width), height: UInt(rect.size.height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
        

        其中 rect 有高度和宽度

        【讨论】:

          【解决方案5】:

          同时兼容 Xcode 8.3Xcode 9 的建议方式,支持 Swift 3Swift 4

          let colorSpace = CGColorSpaceCreateDeviceRGB()
          guard let bitmapContext = CGContext(data: nil, 
                                              width: Int(size.width),
                                              height: Int(size.height),
                                              bitsPerComponent: Int(bitsPerComponent),
                                              bytesPerRow: Int(bytesPerRow),
                                              space: colorSpace,
                                              bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
                                            return nil
              }
          

          【讨论】:

            【解决方案6】:

            我在使用 UInt 的 Swift 1.2 中遇到了一些问题,现在我正在使用 Int 并且它正在工作。 本示例展示了如何将图像转换为灰度图像。

            let imageRect = self.myImage.frame
            
            let colorSpace = CGColorSpaceCreateDeviceGray()
            let width = imageRect.width
            let height = imageRect.height
            
            let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.None.rawValue)
            let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo)
            

            【讨论】:

              【解决方案7】:

              在 Swift 2.2 中:

              let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue).rawValue
              let colorSpace = CGColorSpaceCreateDeviceRGB()
              let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo)
              

              【讨论】:

                【解决方案8】:

                CGBitmapInfo.AlphaInfoMask 不是有效的位图信息。

                尝试设置 CGBitmapInfo.AlphaLast 或 CGBitmapInfo.AlphaFirst。

                【讨论】:

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