使用任何RGB颜色空间时无法获取有效的CGContext? [英] unable to get a valid CGContext when using any RGB color space?

查看:184
本文介绍了使用任何RGB颜色空间时无法获取有效的CGContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个CGContext,可以在其中绘制图像以检索原始像素数据以供以后处理.我正在使用依赖于正在处理的图像的颜色空间来创建CGContext.当我使用灰度或CMYK色彩空间时,此方法工作正常,但在使用任何RGB色彩空间时,我无法获得有效的CGContext.下面的代码片段说明了我所看到的行为(使用Swift 4,XCode 10.1).我必须错过一些显而易见的东西,但对于我的一生,我找不到它.任何帮助将不胜感激!

I'm trying to create a CGContext in which I can draw an image to retrieve the raw pixel data for later processing. I'm creating the CGContext using a color space dependent upon the image I'm processing. This works fine when I use a grayscale or CMYK colorspace, but I can't get a valid CGContext when working with any RGB color space. The snippets below illustrate the behavior I'm seeing (with Swift 4, XCode 10.1). I've got to be missing something obvious but for the life of me I can't find it. Any help would be greatly appreciated!

//let colorSpace3:CGColorSpace = CGColorSpaceCreateDeviceGray() // get good context
//let colorSpace3:CGColorSpace = CGColorSpaceCreateDeviceCMYK() // get good context
//let colorSpace3: CGColorSpace = CGColorSpace(name: CGColorSpace.linearGray)! // get good context
//let colorSpace3: CGColorSpace = CGColorSpace(name: CGColorSpace.genericCMYK)! // get good context
//let colorSpace3:CGColorSpace = CGColorSpaceCreateDeviceRGB() //context3 is nil
//let colorSpace3: CGColorSpace = CGColorSpace(name: CGColorSpace.sRGB)! //context3 is nil
//let colorSpace3: CGColorSpace = CGColorSpace(name: CGColorSpace.linearSRGB)! // context3 is nil
let colorSpace3: CGColorSpace = CGColorSpace(name: CGColorSpace.adobeRGB1998)! // context3 is nil

let bmpinfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue) // bmpinfo different settings doesn't change behavior

let context3 = CGContext(data: nil, width: 256, height: 256, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace3, bitmapInfo: bmpinfo.rawValue)

print(context3) // prints nil whenever colorSpace3 is an RGB space??

推荐答案

您应该在控制台中看到CGBitmapContextCreate: unsupported parameter combination.只是改变

You should be seeing CGBitmapContextCreate: unsupported parameter combination in the console. Just change

let bmpinfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)

let bmpinfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)

,您应该一切顺利.示例:

and you should be good to go. Example:

let colorSpace3: CGColorSpace = CGColorSpace(name: CGColorSpace.sRGB)!
let bmpinfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
let context3 = CGContext(data: nil, width: 256, height: 256, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace3, bitmapInfo: bmpinfo.rawValue)!
print(context3)

打印:

<CGContext 0x600000165580> (kCGContextTypeBitmap)
    <<CGColorSpace 0x60000022a9c0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1)>
        width = 256, height = 256, bpc = 8, bpp = 32, row bytes = 1024 
        kCGImageAlphaPremultipliedLast | 0 (default byte order) 

顺便说一句,您的小转换舞是不必要的.您可以将bmpInfo设置为CGImageAlphaInfo.premultipliedLast.rawValue并将其作为bitmapInfo参数直接传递到函数中.仅在将字节顺序信息与alpha信息组合在一起的情况下,才需要进行转换.

By the way, your little conversion dance is unnecessary. You can just set your bmpInfo to CGImageAlphaInfo.premultipliedLast.rawValue and pass it straight into the function as the bitmapInfo argument. You only need the conversion if you're also combining byte-ordering information with the alpha information.

这篇关于使用任何RGB颜色空间时无法获取有效的CGContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆