0

我有以下代码,我正在尝试准备我拥有的图像,以定义白色像素、透明等在哪里,但我的输出始终为 255 ( if (pointer.pointee >> 24) == 255)所以它告诉我它是全黑的,而它不是!有什么帮助吗?

    var colorSpace = CGColorSpaceCreateDeviceRGB()
    let imageCG = image.cgImage!
    imageWidth = imageCG.width
    imageHeight = imageCG.height
    imageScale = image.scale
    totalPixels = imageHeight * imageWidth

    var bitsPerComponent = 8
    var bytesPerRow = 4 * imageWidth
    var bitmapInfo = CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.premultipliedFirst.rawValue

    // Init imageContext
    imageContext = CGContext(data: nil, width: imageWidth, height: imageHeight, bitsPerComponent: bitsPerComponen`enter code here`t, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo)!
    imageContext.draw(imageCG, in: CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight))


    // Init imageData

    imageData = imageContext.data!.bindMemory(to: UInt32.self, capacity: totalPixels)

    // Init regionsData
    regionsData = UnsafeMutablePointer<UInt16>.allocate(capacity: totalPixels)

    var regionsPointer: UnsafeMutablePointer<UInt16> = regionsData
    var pointer: UnsafeMutablePointer<UInt32> = imageData

    // Scan image data and mark full transparent, semitransparent and opaque pixels in regions data

    let minAlpha = UInt32(255 - transparencyTolerance)

    for _ in 0..<totalPixels {
        if (pointer.pointee >> 24) == 255 {
            regionsPointer.pointee = regionsBlackColor
        } else if (pointer.pointee >> 24) > minAlpha {
            regionsPointer.pointee = regionsTransparentColor
        } else {
            regionsPointer.pointee = regionsWhiteColor
        }
        pointer.pointee = whiteColor
        pointer = pointer.successor()
        regionsPointer = regionsPointer.successor()
    }
4

0 回答 0