1

我正在尝试使用人工创建的数据创建图像并希望使用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)
    }

该代码在模拟器上运行良好,并显示如下:

在此处输入图像描述

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

在此处输入图像描述

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

4

1 回答 1

1

我自己想通了。我仍然不知道为什么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)
    }
于 2019-06-09T22:00:11.887 回答