【问题标题】:Getting Array Data from Image File in Swift在 Swift 中从图像文件中获取数组数据
【发布时间】:2021-09-20 02:41:11
【问题描述】:

我四处搜索,发现了从图像中获取数组数据的各种方法。我已经选择了 CGImage 设置的示例,现在到达了一些产生我期望的输出的代码。留下一个问题。
问题:

无论出于何种原因,我的代码都将数组初始化为零,并且在 for 循环之后,任何超过 ~1000 的索引仍然为零。对于每个单个数组,红色、绿色、蓝色和 alpha 值看起来都符合预期,直到该值 >1000,然后都为零!

看看我的代码,请帮我调试一下。

func forLoopWay() -> [[UInt8]] {
    let startTime = CFAbsoluteTimeGetCurrent()
    print("for loop way")
    let Image = UIImage(named: imageName)
    let image: CGImage = Image!.cgImage!
    
    let width = image.width
    let height = image.height
    let colorspace = CGColorSpaceCreateDeviceRGB()
    let bytesPerRow = (4 * width);
    let bitsPerComponent = 8
    var pixels = UnsafeMutablePointer<UInt8>.allocate(capacity: width * height * 4 )

    let context = CGContext.init(data: pixels,
                                 width: width,
                                 height: height,
                                 bitsPerComponent: bitsPerComponent,
                                 bytesPerRow: bytesPerRow,
                                 space: colorspace,
                                 bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)

    context?.draw(image, in: CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height)))
    
    var alpha = [UInt8].init(repeating: 0, count: width * height)
    var red = [UInt8].init(repeating: 0, count: width * height)
    var green = [UInt8].init(repeating: 0, count: width * height)
    var blue = [UInt8].init(repeating: 0, count: width * height)

    let test_val = pixels.pointee
    print(test_val)
    print( pixels.pointee)
    for x in 0..<width {
        for y in 0..<height {
        red[x + y]    = pixels.pointee
        pixels = pixels + 1
        green[x + y]      = pixels.pointee
        pixels = pixels + 1
        blue[x + y]    = pixels.pointee
        pixels = pixels + 1
        alpha[x + y]     = pixels.pointee
            pixels = pixels + 1
      }
    }
    
    let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
    print("Time elapsed \(String(format : "%0.5f", timeElapsed)) seconds")
    print()
    return [red, green, blue, alpha]
}

我在返回之前设置了一个断点并尝试了这个调试。我的图像是 512 x 512,所以线性索引 1100 为零是没有意义的。

(lldb) print alpha[1003]
(UInt8) $R3 = 255
(lldb) print alpha[1010]
(UInt8) $R4 = 255
(lldb) print alpha[1100]
(UInt8) $R5 = 0
(lldb) print red[1010]
(UInt8) $R6 = 75
(lldb) print red[1010]
(UInt8) $R7 = 75
(lldb) print red[1100]
(UInt8) $R8 = 0
(lldb) print red[1003]
(UInt8) $R9 = 75
(lldb) print red[10]
(UInt8) $R10 = 161

【问题讨论】:

  • 你应该构建你的数据。我的意思是你的Pixel
  • 我犯了一个愚蠢的数学错误。我通过引入var linearOffset = 0 并在每次迭代中递增来修复它。并用这个替换 x + y。总nooby错误。

标签: swift pointers swift5 cgimage rgba


【解决方案1】:

当我打开另一半大脑时,我解决了这个问题。

var linearOffset = 0
for x in 0..<width {
    for y in 0..<height {
        red[linearOffset] = pixels.pointee
        pixels += 1
        green[linearOffset] = pixels.pointee
        pixels += 1
        blue[linearOffset] = pixels.pointee
        pixels += 1
        alpha[linearOffset] = pixels.pointee
        pixels += 1
        linearOffset +=1 
    }
}
  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多