我目前正在使用以下代码来裁剪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_ACCESS
或cropHeight
超出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))
关于如何扩展和填充填充以免崩溃的任何想法?