捕获屏幕并保存到视频ios时接收到内存警告 [英] Received memory warning when capturing screen and save to video ios

查看:392
本文介绍了捕获屏幕并保存到视频ios时接收到内存警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am now writing a program to capture screen and convert to video. I can successfully save the video if it is less than 10 seconds. But, if more than that, I received memory warning and application crash. I wrote this code as follow. Where am I missing to release data ? I would like to know how to do.

-(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(image)。此对象是通过调用 UIImage 对象的 CGImage 方法创建的。因此,所有权未被转移,ARC仍然为您的 img 对象执行内存管理,并且不释放映像对象

I agree that you don't want to do the CGImageRelease(image). This object was created by calling CGImage method of a UIImage object. Thus ownership was not transferred and ARC still does the memory management for your img object and no releasing of the image object is needed.

但我想您想要还原您的 CFRelease(imageData) 。这是一个由 CGDataProviderCopyData 创建的对象,因此您拥有它并且必须清理。

But I think you do want to restore your CFRelease(imageData). This is an object created by CGDataProviderCopyData, so you own it and must clean up.

使用 CVPixelBufferCreateWithBytes appendPixelBuffer pixelBuffer >。您可以使用 CVPixelBufferRelease 函数。

I also think you have to release the pixelBuffer that you created with CVPixelBufferCreateWithBytes after you appendPixelBuffer. You can use the CVPixelBufferRelease function for that.

Core Foundation内存规则是,如果函数< c $ c>复制或在名称中创建,您拥有该对象,并负责释放它。请参阅

The Core Foundation memory rule is that if the function has Copy or Create in the name, you own that object and are responsible for releasing it. See the Create Rule in the Memory Management Programming Guide for Core Foundation.

我会认为静态分析器从Xcode产品菜单中选择 shift + 命令 + B 或Analyze)会发现这个问题,在寻找核心基金会内存问题(虽然,不完美)。

I would have thought that the static analyzer (shift+command+B or "Analyze" from the Xcode "Product" menu) would have identified this issue, as it has gotten much better at finding Core Foundation memory issues (albeit, not perfect).

或者,如果您通过仪器中的泄漏工具运行应用程序(也会同时显示分配工具),您可以查看您的内存使用情况。虽然视频捕获需要大量的Live Bytes,但在我的经验中,它保持平坦。如果它增长,你有一个泄漏在某个地方。

Alternatively, if you run your app through the Leaks tool in Instruments (which will also show you the Allocations tool at the same time), you can take a look at your memory usage. While the video capture requires a lot of Live Bytes, in my experience it stays pretty darn flat. If it's growing, you have a leak somewhere.

这篇关于捕获屏幕并保存到视频ios时接收到内存警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆