是否有标准的执行方式来编辑/绘制CVImageBuffer/CVPixelBuffer?
我在网上找到的所有视频编辑演示都覆盖了屏幕上的绘图(矩形或文本),并且不直接编辑CVPixelBuffer。
我尝试使用CGContext更新,但是保存的视频没有显示上下文绘图
private var adapter: AVAssetWriterInputPixelBufferAdaptor?
extension TrainViewController: CameraFeedManagerDelegate {
func didOutput(sampleBuffer: CMSampleBuffer) {
let time = CMTime(seconds: timestamp - _time, preferredTimescale: CMTimeScale(600))
let pixelBuffer: CVPixelBuffer? = CMSampleBufferGetImageBuffer(sampleBuffer)
guard let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer),
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
space: colorSpace,
bitmapInfo: alphaInfo.rawValue)
else {
return nil
}
context.setFillColor(red: 1, green: 0, blue: 0, alpha: 1.0)
context.fillEllipse(in: CGRect(x: 0, y: 0, width: width, height: height))
context.flush()
adapter?.append(pixelBuffer, withPresentationTime: time)
}
}
发布于 2022-05-02 20:49:37
在绘制完上下文后,您需要在创建位图CVPixelBufferLockBaseAddress(pixelBuffer, 0)
、CGContext
和CVPixelBufferUnlockBaseAddress(pixelBuffer, 0)
之前调用CVPixelBufferUnlockBaseAddress(pixelBuffer, 0)
。
在不锁定像素缓冲区的情况下,CVPixelBufferGetBaseAddress()
返回空。这将导致您的CGContext
分配新的内存,然后将其丢弃。
同时还要检查你的颜色空间。很容易把你的部件混在一起。
例如:
guard
CVPixelBufferLockBaseAddress(pixelBuffer) == kCVReturnSuccess,
let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer),
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
space: colorSpace,
bitmapInfo: alphaInfo.rawValue)
else {
return nil
}
context.setFillColor(red: 1, green: 0, blue: 0, alpha: 1.0)
context.fillEllipse(in: CGRect(x: 0, y: 0, width: width, height: height))
CVPixelBufferUnlockBaseAddress(pixelBuffer)
adapter?.append(pixelBuffer, withPresentationTime: time)
https://stackoverflow.com/questions/71521769
复制相似问题