菜鸟教程小白 发表于 2022-12-11 19:35:15

ios - CGContext.init() -- NULL 颜色空间不再允许


                                            <p><p>TL;DR:在旧版 Obj-C 代码中,颜色空间参数值为 <code>NULL</code>。这在 Swift 等效项中是不允许的。使用什么值?</p>

<p>我继承了如下代码:</p>

<pre><code>unsigned char pixel = {0};
CGContextRef context = CGBitmapContextCreate(
    pixel,1, 1, 8, 1, NULL, (CGBitmapInfo)kCGImageAlphaOnly
);
</code></pre>

<p>到 Swift 4 <code>CGContext</code> 的移植很简单,除了那个 <code>NULL</code> 颜色空间值。使用一个合理的值,我从 <code>CGContext.init?()</code> 得到 <code>nil</code>。我的翻译是:</p>

<pre><code>var pixelValue = UInt8(0)
var pixel = Data(buffer: UnsafeBufferPointer(start:&amp;pixelValue, count:1))
let context = CGContext(
    data            : &amp;pixel,
    width         : 1,
    height          : 1,
    bitsPerComponent: 8,
    bytesPerRow   : 1,
    space         : CGColorSpace(name:CGColorSpace.genericRGBLinear)!,
    bitmapInfo      : CGImageAlphaInfo.alphaOnly.rawValue
)! // Returns nil; unwrapping crashes
</code></pre>

<p><strong>问</strong>:<code>space</code> 的合适值是多少? (我提供的值不返回 <code>nil</code>;它是 <code>CGContext()</code> 调用本身。</p>

<p>设置环境变量 <code>CGBITMAP_CONTEXT_LOG_ERRORS</code> 会产生如下错误日志:</p>

<pre><code>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.
</code></pre>

<p>对于更多背景故事,上下文用于通过以下方式查找 <code>UIImage</code> 中单个像素的 alpha 值:</p>

<pre><code>unsigned char pixel = {0};
CGContextRef context = CGBitmapContextCreate(pixel,1, 1, 8, 1, NULL, (CGBitmapInfo)kCGImageAlphaOnly);
UIGraphicsPushContext(context);
;
UIGraphicsPopContext();
CGContextRelease(context);
CGFloat alpha = pixel/255.0;
</code></pre>

<p>(我确实有可能的替代方法来寻找 alpha,但为了不影响遗留代码,我希望保持这种方式。)</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我最近研究过类似的主题,也许这个代码示例会对某人有所帮助:</p>

<pre><code>let image = UIImage(named: &#34;2.png&#34;)
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&lt;UInt8&gt; = .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..&lt;height {
    for col in 0..&lt;width {
      let alphaValue = bitmapData
      if alphaValue != 0 {
            //visible pixel
      }
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - CGContext.init() -- NULL 颜色空间不再允许,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48195801/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48195801/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - CGContext.init() -- NULL 颜色空间不再允许