I am getting unsupported parameter combination CGBitmap error with swift

前端 未结 8 556
野趣味
野趣味 2020-12-09 08:16

I am trying to create a CGContext in swift. It compiles but throws an error at runtime.

let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let conte         


        
相关标签:
8条回答
  • 2020-12-09 08:24

    Just in case somebody is running into the same problem. The snippet below finally works.

    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)
    

    It generates a 32 bit RGBA context in swift

    0 讨论(0)
  • 2020-12-09 08:26

    Suggested way compatible with both Xcode 8.3 and Xcode 9 which supports Swift 3 and Swift 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
        }
    
    0 讨论(0)
  • 2020-12-09 08:30

    I had some issues in Swift 1.2 using UInt, now I'm using Int and it's working. This Example shows how to convert an Image to a grayscale image.

    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)
    
    0 讨论(0)
  • 2020-12-09 08:32

    In Swift 2.1 one can access the fields properly, and even OR them together:

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

    Whole lot of 'rawValue' going on :)

    You don't even need to separate out the bitmapInfo, and can do a one-liner:

    let context = CGBitmapContextCreate(baseAddress, width, height, 8,
                    bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue
    
    0 讨论(0)
  • 2020-12-09 08:39

    Updated for 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)
    

    where rect has height and width

    0 讨论(0)
  • 2020-12-09 08:43

    In 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)
    
    0 讨论(0)
提交回复
热议问题