使用AVAssetWriter合成视频,内存暴涨,并且合成完不会释放内存,有大神看下改如何优化吗?下面是我的代码
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:self.theVideoPath] fileType:AVFileTypeQuickTimeMovie error:&error];
NSParameterAssert(videoWriter);
if(error)
NSLog(@"error =%@", [error localizedDescription]);
//mov的格式设置 编码格式 宽度 高度
NSDictionary *videoSettings =[NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecTypeH264,AVVideoCodecKey,
[NSNumber numberWithInt:size.width],AVVideoWidthKey,
[NSNumber numberWithInt:size.height],AVVideoHeightKey,nil];
AVAssetWriterInput *writerInput =[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
NSDictionary*sourcePixelBufferAttributesDictionary =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32ARGB],kCVPixelBufferPixelFormatTypeKey,nil];
// AVAssetWriterInputPixelBufferAdaptor提供CVPixelBufferPool实例,
// 可以使用分配像素缓冲区写入输出文件。使用提供的像素为缓冲池分配通常
// 是更有效的比添加像素缓冲区分配使用一个单独的池
AVAssetWriterInputPixelBufferAdaptor *adaptor =[AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
if ([videoWriter canAddInput:writerInput]){
NSLog(@"11111");
}
else{
NSLog(@"22222");
}
[videoWriter addInput:writerInput];
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
//合成多张图片为一个视频文件
dispatch_queue_t dispatchQueue =dispatch_queue_create("mediaInputQueue",NULL);
int __block frame =0;
[writerInput requestMediaDataWhenReadyOnQueue:dispatchQueue usingBlock:^{
while([writerInput isReadyForMoreMediaData])
{
if(frame >=[self.libraryImages count])
{
[writerInput markAsFinished];
[videoWriter finishWriting];
NSString *exportPath = [[self createVideoFile] stringByAppendingPathComponent:[self movieName]];
[self saveVideoWithVideoPath:self.theVideoPath andSavePath:exportPath];
break;
}
CVPixelBufferRef buffer =NULL;
int idx =frame;
NSLog(@"idx==%d",idx);
buffer = (CVPixelBufferRef)[self pixelBufferFromCGImage:[[self.libraryImages objectAtIndex:idx] CGImage] size:size];
if (buffer)
{
if(![adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(frame,fps)])//设置每秒钟播放图片的个数
{
NSLog(@"FAIL");
[writerInput markAsFinished];
[videoWriter finishWriting];
NSString *exportPath = [[self createVideoFile] stringByAppendingPathComponent:[self movieName]];
[self saveVideoWithVideoPath:self.theVideoPath andSavePath:exportPath];
break;
}
else
{
NSLog(@"OK");
frame++;
}
CFRelease(buffer);
}
}
}];