我正在创建这个相机应用程序。因为它是为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秒后,它命中了(A)行,惨遭失败。
失败并显示错误 -536870211,这显然是一个未知错误。
我已经检查了应用程序是否有泄漏,但没有泄漏。
有什么想法吗?
注:我现在发现,只有当我从后置摄像头(HD)写入帧时,问题才会显示出来。如果我切换到前置摄像头VGA(640x480),它可以正常工作。所以这与内存分配有关,但我已经用仪器检查过,应用程序使用了大约 12 MB 的内存,这个值或多或少是恒定的。无泄漏。
该错误kIOReturnNoMemory
Memory can't be allocated (0xe00002bd).Value: 0xe00002bd (-536870211)
所以听起来你使用了太多的内存。您需要确保在尽可能短的时间内保留尽可能少的样品缓冲液及其衍生物。
Instruments应用程序内存和GPU工具以及Xcode的内存仪表也应该有所帮助。