我正在创建这个是相机的应用程序。因为它是为 iOS 9 创建的,所以我必须在旧设备上对其进行测试。在这种情况下是 iPad 3。该应用程序在新的 iPad Pro 9.7 上完美运行,但在 iPad 3 上一段时间后无法写入视频。
发生的情况是,应用程序开始正常写入帧但突然失败。
我正在使用这种方法来存储每一帧:
- (void)writeToFileFrame:(CIImage *) finalImage
withSampleTime:(CMSampleTimingInfo)sampleTime
andSize:(CGSize)size
{
if (!_assetWriter) {
if (![self initializeAssetWriter]) {
return;
}
}
// convert CIImage to CMSampleBufferRef and save
CGRect extent = [finalImage extent];
CGFloat width = CGRectGetWidth(extent);
CGFloat height = CGRectGetHeight(extent);
if ((width == 0) && (height == 0)) return;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSDictionary dictionary], (id)kCVPixelBufferIOSurfacePropertiesKey,
@(YES), kCVPixelBufferCGImageCompatibilityKey,
@(YES), kCVPixelBufferCGBitmapContextCompatibilityKey,
kCVImageBufferYCbCrMatrix_ITU_R_601_4, kCVImageBufferYCbCrMatrixKey,
kCVImageBufferColorPrimaries_ITU_R_709_2, kCVImageBufferColorPrimariesKey, nil];
// Initialize the video input if this is not done yet
if (!_readyToWriteVideo) {
_readyToWriteVideo = [self setupAssetWriterVideoInputWithSize:size];
}
CVPixelBufferRef pixelBuffer = NULL;
CVPixelBufferCreate(kCFAllocatorSystemDefault, width, height, kCVPixelFormatType_32BGRA, (__bridge CFDictionaryRef) options, &pixelBuffer);
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
[_ciContext render:finalImage toCVPixelBuffer:pixelBuffer];
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );
CMVideoFormatDescriptionRef videoInfo = NULL;
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, &videoInfo);
CMSampleBufferRef oBuf;
OSStatus status = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, videoInfo, &sampleTime, &oBuf);
CVPixelBufferRelease(pixelBuffer);
CFRelease(videoInfo);
if (status != noErr) {
NSLog(@"error creating CMSampleBufferCreateForImageBuffer");
CFRelease(oBuf);
return;
}
// Write video data to file only when all the inputs are ready
if ([self inputsReadyToWriteToFile]) {
if (_assetWriter.error) {
NSLog(@"%@",[_assetWriter.error localizedDescription]);
return;
}
[self writeSampleBuffer:oBuf ofType:AVMediaTypeVideo];
}
CFRelease(oBuf);
}
- (void)writeSampleBuffer:(CMSampleBufferRef)sampleBuffer ofType:(NSString *)mediaType
{
if ( _assetWriter.status == AVAssetWriterStatusUnknown ) {
NSLog(@"unknown state");
// If the asset writer status is unknown, implies writing hasn't started yet, hence start writing with start time as the buffer's presentation timestamp
if ([_assetWriter startWriting]) {
[_assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];
} else {
// error
}
}
if ( _assetWriter.status == AVAssetWriterStatusWriting ) {
// If the asset writer status is writing, append sample buffer to its corresponding asset writer input
if (mediaType == AVMediaTypeVideo) {
if (_assetWriterVideoInput.readyForMoreMediaData) {
if (![_assetWriterVideoInput appendSampleBuffer:sampleBuffer]) {
NSLog(@"error: %@", [_assetWriter.error localizedFailureReason]); //Ⓐ
}
}
}
else if (mediaType == AVMediaTypeAudio) {
if (_assetWriterAudioInput.readyForMoreMediaData) {
if (![_assetWriterAudioInput appendSampleBuffer:sampleBuffer]) {
// error
}
}
}
}
if ( _assetWriter.status == AVAssetWriterStatusFailed ) {
NSLog(@"error");
}
}
这在 iPad Pro 9.7 上运行良好,但在 iPad 3 上,在正确写入帧约 5 秒后,它击中 Ⓐ 行,惨遭失败。
失败并出现错误 -536870211,这显然是一个未知错误。
我检查了应用程序是否有泄漏,但没有。
有任何想法吗?
注意:我现在发现问题仅在我从后置摄像头 (HD) 写入帧时才会显示。如果我切换到前置摄像头 VGA (640x480),它工作正常。所以它与内存分配有关,但我已经检查过仪器和应用程序正在使用大约 12 MB 的内存,这个值或多或少是恒定的。没有泄漏。