捕获屏幕时收到内存警告并保存到视频ios

我现在正在编写一个程序来捕获屏幕并转换为视频.如果视频少于10秒,我可以成功保存视频.但是,如果不止于此,我收到内存警告和应用程序崩溃.我写了这段代码如下.我错过了哪里发布数据?我想知道怎么做.

-(void)captureAndSaveImage
{

if(!stopCapturing){

if (assetWriterInput.readyForMoreMediaData)
{
    keepTrackOfBackGroundMood++;
    NSLog(@"keepTrackOfBackGroundMood is %d",keepTrackOfBackGroundMood);

    CVReturn cvErr = kCVReturnSuccess;

    CGSize imageSize = screenCaptureAndDraw.bounds.size;


    CGFloat imageScale = 0; //if zero, it reduce processing time

    if (NULL != UIGraphicsBeginImageContextWithOptions)
    {
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, imageScale);
    }
    else
    {
        UIGraphicsBeginImageContext(imageSize);
    }

    [self.hiddenView.layer renderInContext:UIGraphicsGetCurrentContext()];

    [self.screenCaptureAndDraw.layer renderInContext:UIGraphicsGetCurrentContext()];


    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();



     image = (CGImageRef) [img CGImage];

    CVPixelBufferRef pixelBuffer = NULL;
    CFDataRef imageData= CGDataProviderCopyData(CGImageGetDataProvider(image));
    cvErr = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,
                                         FRAME_WIDTH2,
                                         FRAME_HEIGHT2,
                                         kCVPixelFormatType_32BGRA,
                                         (void*)CFDataGetBytePtr(imageData),
                                         CGImageGetBytesPerRow(image),
                                         NULL,
                                         NULL,
                                         NULL,
                                         &pixelBuffer);


    //CFRelease(imageData);
    //CGImageRelease(image);  //I can't write this code because I am not creating it and when I check online, it say it is not my responsibility to release. If I write, the application crash immediately


    // calculate the time
    CFAbsoluteTime thisFrameWallClockTime = CFAbsoluteTimeGetCurrent();
    CFTimeInterval elapsedTime = thisFrameWallClockTime - firstFrameWallClockTime;


    // write the sample

    BOOL appended = [assetWriterPixelBufferAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:presentationTime];

    if (appended) {
        NSLog (@"appended sample at time %lf and keepTrackofappended is %d", CMTimeGetSeconds(presentationTime),keepTrackofappended);
        keepTrackofappended++;
    } else {
        NSLog (@"failed to append");
        [self stopRecording];
        //self.startStopButton.selected = NO;
        screenRecord=false;
    }



}

}//stop capturing
// });


}

最佳答案 我同意你不想做CGImageRelease(图片).该对象是通过调用UIImage对象的CGImage方法创建的.因此,所有权未被转移,ARC仍然为您的img对象进行内存管理,并且不需要释放图像对象.

但我认为你确实想恢复你的CFRelease(imageData).这是由CGDataProviderCopyData创建的对象,因此您拥有它并且必须清理.

我还认为你必须在appendPixelBuffer之后释放你用CVPixelBufferCreateWithBytes创建的pixelBuffer.您可以使用CVPixelBufferRelease函数.

Core Foundation内存规则是,如果函数在名称中具有“复制”或“创建”,则您拥有该对象并负责释放它.请参阅“Core Foundation内存管理编程指南”中的Create Rule.

我原以为静态分析器(移位命令B或Xcode“产品”菜单中的“分析”)会识别出这个问题,因为它在查找Core Foundation内存问题方面已经有了更好的表现(尽管不完美).

或者,如果您通过Instruments中的Leaks工具运行您的应用程序(它也会同时显示分配工具),您可以查看您的内存使用情况.虽然视频捕获需要大量的Live Bytes,但根据我的经验,它仍然非常平淡.如果它在增长,你就会在某个地方泄漏.

点赞