gpt4 book ai didi

iphone - iPhone 中 AVAssetWriter 的内存管理问题?

转载 作者:搜寻专家 更新时间:2023-10-30 19:47:08 28 4
gpt4 key购买 nike

我已经使用 AVAssetWriter 从 uiimages 成功创建了视频。但是一旦作者开始编写视频,仪器中的内存分配就会突然增加。内存分配的峰值从 3-4 MB 变为 120MB,然后冷却下来。我为此使用了以下代码...

-(void)writeImageAsMovie:(NSArray *)array toPath:(NSString*)path size:(CGSize)size
{
NSMutableDictionary *attributes = [[NSMutableDictionary alloc]init];
[attributes setObject:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32ARGB] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];
[attributes setObject:[NSNumber numberWithUnsignedInt:320] forKey:(NSString*)kCVPixelBufferWidthKey];
[attributes setObject:[NSNumber numberWithUnsignedInt:416] forKey:(NSString*)kCVPixelBufferHeightKey];

NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
error:&error];
NSParameterAssert(videoWriter);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:size.width], AVVideoWidthKey,
[NSNumber numberWithInt:size.height], AVVideoHeightKey,
nil];

AVAssetWriterInput* writerInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain];

adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
sourcePixelBufferAttributes:attributes];

NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];


//Start a session:
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];

CVPixelBufferRef buffer = NULL;
buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:0] CGImage]];
[adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];

//Write samples:
for (int i = 0;i<[array count]; i++)
{
if([writerInput isReadyForMoreMediaData])
{
NSLog(@"inside for loop %d",i);
CMTime frameTime = CMTimeMake(1, 20);

CMTime lastTime=CMTimeMake(i, 20); //i is from 0 to 19 of the loop above

CMTime presentTime=CMTimeAdd(lastTime, frameTime);

buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:i] CGImage]];

[adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];

if(buffer)
CVBufferRelease(buffer);
}
else
{
NSLog(@"error");
i--;
}

}

//Finish the session:
[writerInput markAsFinished];
[videoWriter finishWriting];

NSURL *pathURL = [NSURL fileURLWithPath:path];

AVURLAsset *url = [[AVURLAsset alloc] initWithURL:pathURL options:nil];

[clipsArray addObject:url];
[url release];
CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
[videoWriter release];
[writerInput release];
[imageArray removeAllObjects];
}

任何人都可以帮助解决这个问题,因为我在过去 2 天遇到了问题......

提前致谢...

最佳答案

我认为问题是你在一个循环中运行它,没有给 RunLoop 任何改变来对自动释放的实例进行垃圾收集。

关于iphone - iPhone 中 AVAssetWriter 的内存管理问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4069544/

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