gpt4 book ai didi

ios - 将视频写入文件在 iPad 3 上正常工作约 5 秒,然后失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:33:38 25 4
gpt4 key购买 nike

我正在创建这个应用程序,它是一个相机。因为它是为 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 的内存,这个值或多或少是恒定的。没有泄漏。

最佳答案

那个错误是kIOReturnNoMemory

Memory can't be allocated (0xe00002bd).Value: 0xe00002bd (-536870211)

听起来你用的内存太多了。您需要确保在尽可能短的时间内保留尽可能少的样本缓冲区及其衍生物。

Instruments 应用程序内存和 GPU 工具以及 Xcode 的内存量表也应该有帮助。

关于ios - 将视频写入文件在 iPad 3 上正常工作约 5 秒,然后失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43048310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com