在iOS中使用dispatch_async时出现内存警告

在iOS中使用dispatch_async时出现内存警告,ios,grand-central-dispatch,avassetwriter,Ios,Grand Central Dispatch,Avassetwriter,我有下面的代码,以30fps的速度捕获jpeg帧,并以mp4格式录制视频。我正在尝试将processFrame方法包装在dispatch_async调用中,以便录制过程不会锁定视频播放器。问题是我得到了2级内存警告,几秒钟后应用程序最终崩溃。我可以看到dispatch_async方法在内存中加载队列,因为它试图将每个帧追加到录制的视频输出中,并且以30fps的速度,它没有足够的时间来处理帧并释放已使用的内存。我尝试使用dispatch_after延迟processFrame的执行,但没有帮助。有

我有下面的代码,以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];});
    }
}
processFrame方法

//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 );
        }
    }

}
//用于处理要录制的每个帧的函数
-(void)processFrame:(UIImage*)图像{
if(myRecorder.frameCounter
已解决!我发现我可以使用dispatch\u async\u信号量来防止队列因太多请求而过载,这样队列就没有足够的时间释放分配的资源

以下是我的更新代码:

    long success = dispatch_semaphore_wait(recordingSemaphore, DISPATCH_TIME_FOREVER);

    if (success != 0 )
    {
        NSLog(@"Frame skipped");
    }
    else
    {
        dispatch_async(recordingQueue,^{
            dispatch_semaphore_signal(recordingSemaphore);
            [self processFrame:image];
        });

    }
在我的代码中某处创建的dispatch_信号量。在这里,我告诉信号灯先接受最多50个请求,然后在接受更多请求之前完成处理

dispatch_semaphore_t recordingSemaphore =  dispatch_semaphore_create((long) 50); //so far stable at 50

仍在寻找解决方案。信号不应该是经过处理后发送的吗?[自处理帧:图像];然后发送信号量信号(记录信号量);?