[英]Received memory warning when capturing screen and save to video 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(image)
。 该对象是通过调用UIImage
对象的CGImage
方法创建的。 因此,所有权没有转移,ARC仍然为您的img
对象进行内存管理,并且不需要释放image
对象。
但是我认为您确实想恢复CFRelease(imageData)
。 这是CGDataProviderCopyData
创建的对象,因此您拥有它并且必须清除。
我还认为您必须在CVPixelBufferCreateWithBytes
之后释放使用pixelBuffer
创建的appendPixelBuffer
。 您可以CVPixelBufferRelease
使用CVPixelBufferRelease
函数。
Core Foundation内存规则是,如果函数名称中具有Copy
或Create
,则您拥有该对象并负责释放该对象。 请参阅《 Core Foundation内存管理编程指南 》中的“ 创建规则 ”。
我本以为静态分析器(Xcode“产品”菜单中的shift + 命令 + B或“ Analyze”)会发现此问题,因为它在查找Core Foundation内存问题方面已经变得更好(尽管并不完美)。 。
另外,如果您通过Instruments中的Leaks工具运行应用程序(还将同时显示Allocations工具),则可以查看一下内存使用情况。 虽然视频捕获需要大量的实时字节,但以我的经验来看,它仍然相当平坦。 如果它正在增长,那么您的某处会漏水。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.