我正在使用应用程序中的功能,将图像从示例缓冲区写入avassetwriter。奇怪的是,这在10.5" iPad pro 上都可以正常工作,但会在A 7.9" iPad mini 2 上引起崩溃。我无法理解在两个不同的设备上如何有问题的相同代码。但这是我的代码;
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
// Setup the pixel buffer image
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
// Setup the format description
let formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer)!
// Setup the current video dimensions
self.currentVideoDimensions = CMVideoFormatDescriptionGetDimensions(formatDescription)
// Setup the current sample time
self.currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer)
// Handle record
if self.isCapturing {
// Setup auto release pool
autoreleasepool {
// Setup the output image
let outputImage = CIImage(cvPixelBuffer: pixelBuffer)
// Ensure the video writer is ready for more data
if self.videoWriter?.assetWriterPixelBufferInput?.assetWriterInput.isReadyForMoreMediaData == true {
// Setup the new pixel buffer (THIS IS WHERE THE ERROR OCCURS)
var newPixelBuffer: CVPixelBuffer? = nil
// Setup the pixel buffer pool
CVPixelBufferPoolCreatePixelBuffer(nil, (self.videoWriter?.assetWriterPixelBufferInput!.pixelBufferPool!)!, &newPixelBuffer)
// Render the image to context
self.context.render(outputImage, to: newPixelBuffer!, bounds: outputImage.extent, colorSpace: nil)
// Setup a success case
let success = self.videoWriter?.assetWriterPixelBufferInput?.append(newPixelBuffer!, withPresentationTime: self.currentSampleTime!)
// Ensure the success case exists
guard let mySuccess = success else { return }
// If unsuccessful, log
if !mySuccess {
print("Error with the sample buffer. Check for dropped frames.")
}
}
}
}
}
我收到一个错误, newPixelBuffer 是零,但同样,仅在7.9英寸ipad上。iPad Pro函数没有任何错误。
我最终通过在资产作者的视频输出设置中将问题追溯到我选择的编解码器来解决此问题。我将编解码器设置为:
let codec: AVVideoCodecType = AVVideoCodecType.hevc
在进行一些研究时,我发现了这篇文章,该文章表明只有某些设备才能在HEVC中捕获媒体。由于我的第一个设备是10.5" iPad Pro,因此毫无问题地捕获了媒体。我的第二个设备是iPad mini,每次我尝试捕获时都会出现原始问题。
自那以后,我的编解码器选择更改为:
let codec: AVVideoCodecType = AVVideoCodecType.h264
,问题现在已经消失。