我有下面的代码,它以 30fps 的速度捕获 jpeg 帧并以 mp4 格式记录视频。我正在尝试将 processFrame 方法包装在 dispatch_async 调用中,以便录制过程不会锁定视频播放器。问题是我收到了 2 级内存警告,应用程序最终会在几秒钟后崩溃。我可以看到 dispatch_async 方法将队列加载到内存中,因为它试图将每一帧附加到录制的视频输出中,并且在 30fps 时,它没有足够的时间来处理帧并释放使用的内存。我尝试使用 dispatch_after 来延迟 processFrame 的执行,但它没有帮助。有任何想法吗?我应该以不同的方式做这件事吗?
这个方法每秒被调用大约 30 次。
//Process the data sent by the server and send follow-up commands if needed
-(void)processServerData:(NSData *)data{
//render the video in the UIImage control
UIImage *image =[UIImage imageWithData:data];
imageCtrl.image = image;
//record the frame in the background
dispatch_async(recordingQueue,^{[self processFrame:image];});
}
}
处理框架方法
//function for processing each frame for recording
-(void) processFrame:(UIImage *) image {
if (myRecorder.frameCounter < myRecorder.maxFrames)
{
if([myRecorder.writerInput isReadyForMoreMediaData])
{
CMTime frameTime = CMTimeMake(1, myRecorder.timeScale);
CMTime lastTime=CMTimeMake(myRecorder.frameCounter, myRecorder.timeScale);
CMTime presentTime=CMTimeAdd(lastTime, frameTime);
buffer = [Recorder pixelBufferFromCGImage:image.CGImage size:myRecorder.imageSize];
if(buffer)
{
[myRecorder.adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];
myRecorder.frameCounter++;
CVBufferRelease(buffer);
if (myRecorder.frameCounter==myRecorder.maxFrames)
{
[myRecorder finishSession];
myRecorder.frameCounter=0;
myRecorder.isRecording = NO;
}
}
else
{
NSLog(@"Buffer is empty");
}
}
else
{
NSLog(@"adaptor not ready frameCounter=%d ",myRecorder.frameCounter );
}
}
}