Empty CGContext

送分小仙女□ 提交于 2021-01-29 03:18:03

问题


In Objective-C I was able to use CGBitmapContextCreate to create an empty context. I am trying to to the same in Swift 3, but for some reason it is nil. What am I missing?

let inImage: UIImage = ...
let width = Int(inImage.size.width)
let height = Int(inImage.size.height)

let bitmapBytesPerRow = width * 4
let bitmapByteCount = bitmapBytesPerRow * height

let pixelData = UnsafeMutablePointer<UInt8>.allocate(capacity: bitmapByteCount)

let context = CGContext(data: pixelData,
                                width: width,
                                height: height,
                                bitsPerComponent: 8,
                                bytesPerRow: bitmapBytesPerRow,
                                space: CGColorSpaceCreateDeviceRGB(),
                                bitmapInfo: CGImageAlphaInfo.alphaOnly.rawValue)

回答1:


I'm not sure what the code in the liked article would do, but two things are different with your Swift code.

  • bytesPerRow: width // width * 4 (== bitmapBytesPerRow)
  • space : NULL // CGColorSpaceCreateDeviceRGB()

The documentation of CGBitmapContextCreate does not say anything about supplying NULL for colorspace, but the header doc says The number of components for each pixel is specified by space, so, at least, CGColorSpaceCreateDeviceRGB() is not appropriate for alphaOnly (which should have only 1 component per pixel).

As far as I tested, this code returns non-nil CGContext:

    let bitmapBytesPerRow = width //<-
    let bitmapByteCount = bitmapBytesPerRow * height

    let pixelData = UnsafeMutablePointer<UInt8>.allocate(capacity: bitmapByteCount)

    let context = CGContext(data: pixelData,
                            width: width,
                            height: height,
                            bitsPerComponent: 8,
                            bytesPerRow: bitmapBytesPerRow,
                            space: CGColorSpaceCreateDeviceGray(), //<-
                            bitmapInfo: CGImageAlphaInfo.alphaOnly.rawValue)

But, not sure if this works for your purpose or not.




回答2:


I was working on this thing and faced same issue. The solution I found is to use

var colorSpace =   CGColorSpace.init(name: CGColorSpace.sRGB)!
  let context = CGContext(data: nil,
                            width: Int(outputSize.width),
                            height: Int(outputSize.height),
                            bitsPerComponent: self.bitsPerComponent,
                            bytesPerRow: bitmapBytesPerRow,
                            space: colorSpace,
                            bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)

Actually my image's color space was Indexed Which can't be used to make a context. So instead of using image's own colorSpace I made my own by using

var colorSpace =   CGColorSpace.init(name: CGColorSpace.sRGB)!

and passed it to the context. it resolved my error (nil context issue).



来源:https://stackoverflow.com/questions/41100895/empty-cgcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!