【问题标题】:iOS Core Graphics - how to render UIImage to sample pixel color with 16 bit components, not 8 bit?iOS Core Graphics - 如何渲染 UIImage 以使用 16 位组件而不是 8 位来采样像素颜色?
【发布时间】:2021-07-24 08:23:33
【问题描述】:

我有一个来自ARKitUIImage。我希望能够以高精度对该图像的颜色进行采样。搜索 UIImage 或 CGImage 的示例颜色返回如下代码,这给了我四个 UInt8 组件。

我尝试更改每个组件的位数,但不知道如何调整 CGContext 的其他参数以使其呈现。

如何指定 CoreGraphics 使用 16 位颜色组件渲染 UIImage? (或者不是 UInt8 的东西?)

let result = renderer.image { imageRendererContext in
    
    let context = imageRendererContext.cgContext

    context.setStrokeColor(UIColor.clear.cgColor)

    let maskWidth  = Int(mask.size.width)
    let maskHeight = Int(mask.size.height)
    
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let bytesPerPixel = 4
    let bytesPerRow = bytesPerPixel * maskWidth
    let bitsPerComponent = 8
    let bitmapInfo: UInt32 = CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue
    
    guard let maskContext = CGContext(data: nil,
                                      width: maskWidth,
                                      height: maskHeight,
                                      bitsPerComponent: bitsPerComponent,
                                      bytesPerRow: bytesPerRow,
                                      space: colorSpace,
                                      bitmapInfo: bitmapInfo),
          let maskPointer = maskContext.data?.assumingMemoryBound(to: UInt8.self) else {
        return
    }
    
    maskContext.draw(maskCGImage, in: CGRect(x: 0, y: 0, width: maskWidth, height: maskHeight))

    for x in 0 ..< maskWidth {
        for y in 0 ..< maskHeight {

            let i = bytesPerRow * Int(y) + bytesPerPixel * Int(x)
            
            let a = CGFloat(maskPointer[i + 3]) / 255.0
            let r = (CGFloat(maskPointer[i]) / a) / 255.0
            let g = (CGFloat(maskPointer[i + 1]) / a) / 255.0
            let b = (CGFloat(maskPointer[i + 2]) / a) / 255.0
        }
    }
}

【问题讨论】:

    标签: core-graphics swift5 ios14 cgimage color-space


    【解决方案1】:

    发现 described in the core graphics guide, 每个组件 16 位及更高仅适用于 MacOS,不适用于 iOS

    【讨论】:

      猜你喜欢
      • 2015-05-07
      • 2020-12-09
      • 1970-01-01
      • 2021-05-25
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      相关资源
      最近更新 更多