CVPixelBuffer导致在设备上产生垃圾图像,同时在模拟器上按预期工作

时间:2019-06-06 19:10:21

标签: ios swift cvpixelbuffer

我正在尝试使用人工创建的数据创建图像,并想使用CVPixelBuffer

    private func RGBAImage(width w: Int, height h: Int) -> UIImage? {

        let width = w * Int(UIScreen.main.scale)
        let height = h * Int(UIScreen.main.scale)

        // Prepare artificial data

        let dataPtr = UnsafeMutablePointer<UInt8>.allocate(capacity: width * height * 4)

        for i in 0..<width {
            for j in 0..<height {
                dataPtr[4 * (i + j * width)] = UInt8(sin(Double(i) * 0.01 * .pi / Double(UIScreen.main.scale)) * 127 + 127)
                dataPtr[4 * (i + j * width) + 1] = UInt8(255)
                dataPtr[4 * (i + j * width) + 2] = UInt8(0)
                dataPtr[4 * (i + j * width) + 3] = UInt8(0)
            }
        }

        // Convert data into CVPixelBuffer

        var pxBuffer: CVPixelBuffer?

        CVPixelBufferCreateWithBytes(
            kCFAllocatorDefault,
            width,
            height,
            kCVPixelFormatType_32ARGB,
            dataPtr,
            width * 4,
            nil,
            nil,
            [kCVPixelBufferIOSurfacePropertiesKey: [:]] as CFDictionary,
            &pxBuffer
        )

        dataPtr.deallocate()

        guard let cvPxBuffer = pxBuffer else {
            return nil
        }

        // Generate image from CVPixelBuffer

        let ciImage = CIImage(cvImageBuffer: cvPxBuffer)

        return UIImage(ciImage: ciImage, scale: UIScreen.main.scale, orientation: .up)
    }

该代码在模拟器上工作正常,并显示如下:

enter image description here

但是相同的代码显示了设备上的垃圾结果:

enter image description here

我在这里想念什么?欢迎任何建议。

1 个答案:

答案 0 :(得分:0)

弄清楚自己。我仍然不知道为什么CVPixelBufferCreateWithBytes不起作用,但是我可以通过使用CVPixelBufferCreate创建像素缓冲区并逐个设置每个RGB地址的值来使其工作。这也是一种更好的方法,因为我不需要先创建一个数组。

这是设备和模拟器的工作代码:

    private func RGBAImage(width w: Int, height h: Int) -> UIImage? {

        let width = w * Int(UIScreen.main.scale)
        let height = h * Int(UIScreen.main.scale)
        let bytesPerPixel = 4

        // Create CVPixelBuffer with artificial data

        var pxBuffer: CVPixelBuffer?

        CVPixelBufferCreate(
            kCFAllocatorDefault,
            width,
            height,
            kCVPixelFormatType_32ARGB,
            nil,
            &pxBuffer)

        guard let cvPxBuffer = pxBuffer else {
            return nil
        }

        CVPixelBufferLockBaseAddress(cvPxBuffer, CVPixelBufferLockFlags(rawValue: 0))
        let bufferWidth = Int(CVPixelBufferGetWidth(cvPxBuffer))
        let bufferHeight = Int(CVPixelBufferGetHeight(cvPxBuffer))
        let bytesPerRow = CVPixelBufferGetBytesPerRow(cvPxBuffer)

        guard let baseAddress = CVPixelBufferGetBaseAddress(pxBuffer!) else {
            return nil
        }

        for row in 0..<bufferHeight {
            var pixel = baseAddress + row * bytesPerRow
            for col in 0..<bufferWidth {
                let alpha = pixel
                alpha.storeBytes(of: UInt8(sin(Double(col) * 0.01 * .pi / Double(UIScreen.main.scale)) * 127 + 127), as: UInt8.self)

                let red = pixel + 1
                red.storeBytes(of: 255, as: UInt8.self)

                let green = pixel + 2
                green.storeBytes(of: 0, as: UInt8.self)

                let blue = pixel + 3
                blue.storeBytes(of: 0, as: UInt8.self)

                pixel += bytesPerPixel;
            }
        }

        CVPixelBufferUnlockBaseAddress(cvPxBuffer, CVPixelBufferLockFlags(rawValue: 0))

        // Generate image from CVPixelBuffer
        let ciImage = CIImage(cvImageBuffer: cvPxBuffer)

        return UIImage(ciImage: ciImage, scale: UIScreen.main.scale, orientation: .up)
    }