裁剪CVPixelBuffer并使用零填充扩展溢出

时间:2018-12-06 20:56:45

标签: ios swift cvpixelbuffer

我目前正在使用以下代码来裁剪CVPixelBuffer,并且在大多数情况下都可以使用。

let srcData = CVPixelBufferGetBaseAddress(srcPixelBuffer)
let srcBytesPerRow = CVPixelBufferGetBytesPerRow(srcPixelBuffer)

let offset = cropY*srcBytesPerRow + cropX*4
var srcBuffer = vImage_Buffer(data: srcData.advanced(by: offset),
                              height: vImagePixelCount(cropHeight),
                              width: vImagePixelCount(cropWidth),
                              rowBytes: srcBytesPerRow)

但是,我认为当EXEC_BAD_ACCESScropHeight超出cropWidth的高度和宽度时,这将导致srcPixelbuffer错误。我相信是这样,因为下面的代码会不时崩溃。

let destBytesPerRow = scaleWidth*4
guard let destData = malloc(scaleHeight*destBytesPerRow) else {
    print("Error: out of memory")
    return nil
}
var destBuffer = vImage_Buffer(data: destData,
                               height: vImagePixelCount(scaleHeight),
                               width: vImagePixelCount(scaleWidth),
                               rowBytes: destBytesPerRow)

// crashes on the next line.
let error = vImageScale_ARGB8888(&srcBuffer, &destBuffer, nil, vImage_Flags(0))

关于如何扩展和填充填充以免崩溃的任何想法?

0 个答案:

没有答案