CGContext.init()-不再允许使用NULL颜色空间 [英] CGContext.init() -- NULL color space no longer allowed
问题描述
TL; DR:在旧式Obj-C代码中,颜色空间参数值为 NULL
。 Swift等效项中不允许这样做。使用什么值?
TL;DR: In legacy Obj-C code, the color space param value was NULL
. That is not allowed in the Swift equivalent. What value to use?
我继承了以下代码:
unsigned char pixel[1] = {0};
CGContextRef context = CGBitmapContextCreate(
pixel,1, 1, 8, 1, NULL, (CGBitmapInfo)kCGImageAlphaOnly
);
Swift 4的端口 CGContext
很简单,除了 NULL
颜色空间值。使用一个合理的值,我从 CGContext.init?()
得到了 nil
。我的翻译是:
The port to Swift 4 CGContext
is straightforward, except for that NULL
color space value. Using a plausible value, I am getting nil
back from CGContext.init?()
. My translation is:
var pixelValue = UInt8(0)
var pixel = Data(buffer: UnsafeBufferPointer(start:&pixelValue, count:1))
let context = CGContext(
data : &pixel,
width : 1,
height : 1,
bitsPerComponent: 8,
bytesPerRow : 1,
space : CGColorSpace(name:CGColorSpace.genericRGBLinear)!,
bitmapInfo : CGImageAlphaInfo.alphaOnly.rawValue
)! // Returns nil; unwrapping crashes
Q :<$ c $的合适值是多少c>空格? (我提供的值没有返回 nil
;它是 CGContext()
本身。
Q: What is the appropriate value for space
? (The value I provide is not returning nil
; it's the CGContext()
call itself.
设置环境变量 CGBITMAP_CONTEXT_LOG_ERRORS
会产生如下错误日志:
Setting the environment variable CGBITMAP_CONTEXT_LOG_ERRORS
yields an error log like this:
Assertion failed: (0), function get_color_model_name,
file /BuildRoot/Library/Caches/com.apple.xbs/Sources/Quartz2D_Sim/
Quartz2D-1129.2.1/CoreGraphics/API/CGBitmapContextInfo.c, line 210.
对于某些背景故事,上下文用于通过以下方式在 UIImage
中找到单个像素的alpha值:
For some more backstory, the context was used to find the alpha value of a single pixel in a UIImage
in the following way:
unsigned char pixel[1] = {0};
CGContextRef context = CGBitmapContextCreate(pixel,1, 1, 8, 1, NULL, (CGBitmapInfo)kCGImageAlphaOnly);
UIGraphicsPushContext(context);
[image drawAtPoint:CGPointMake(-point.x, -point.y)];
UIGraphicsPopContext();
CGContextRelease(context);
CGFloat alpha = pixel[0]/255.0;
(我确实有找到alpha的替代方法,但是为了不遗留遗留代码,希望保持这种方式。)
(I do have possible alternatives for finding alpha, but in the interest of leaving legacy code alone, would like to keep it this way.)
推荐答案
我最近处理过类似的主题,也许此代码示例将帮助某人:
I recently worked with similar topic, maybe this code sample will help someone:
let image = UIImage(named: "2.png")
guard let cgImage = image?.cgImage else {
fatalError()
}
let width = cgImage.width
let height = cgImage.height
//CGColorSpaceCreateDeviceGray - 1 component, 8 bits
//i.e. 1px = 1byte
let bytesPerRow = width
let bitmapByteCount = width * height
let bitmapData: UnsafeMutablePointer<UInt8> = .allocate(capacity: bitmapByteCount)
defer {
bitmapData.deallocate()
}
bitmapData.initialize(repeating: 0, count: bitmapByteCount)
guard let context = CGContext(data: bitmapData, width: width, height: height,
bitsPerComponent: 8, bytesPerRow: bytesPerRow,
space: CGColorSpaceCreateDeviceGray(), bitmapInfo: CGImageAlphaInfo.alphaOnly.rawValue) else {
fatalError()
}
//draw image to context
var rect = CGRect(x: 0, y: 0, width: width, height: height)
context.draw(cgImage, in: rect)
// Enumerate through all pixels
for row in 0..<height {
for col in 0..<width {
let alphaValue = bitmapData[row * width + col]
if alphaValue != 0 {
//visible pixel
}
}
}
这篇关于CGContext.init()-不再允许使用NULL颜色空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!