Iphone 使用AVCaptureStillImageOutput时出现内存不足警告
我在Iphone 使用AVCaptureStillImageOutput时出现内存不足警告,iphone,memory-management,avcapturesession,Iphone,Memory Management,Avcapturesession,我在[self-setImageData:[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]]上观察到25MB的内存突发 下面的代码片段捕获图片并提供数据的jpg格式。当我们拍摄更多照片时,系统会报告内存不足警告 在profiler中,我们看不到泄漏,但有时应用程序会报告内存不足警告,即使是在20MB时,应用程序也会崩溃 [[self stillImageOutput] captureS
[self-setImageData:[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]]上观察到25MB的内存突发代码>
下面的代码片段捕获图片并提供数据的jpg格式。当我们拍摄更多照片时,系统会报告内存不足警告
在profiler中,我们看不到泄漏,但有时应用程序会报告内存不足警告,即使是在20MB时,应用程序也会崩溃
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
[[self captureSession] stopRunning];
if (imageSampleBuffer != NULL)
{
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments)
{
NSLog(@"attachements: %@", exifAttachments);
}
[self setImageData:[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]];
}
imageSampleBuffer = nil;
// call the designated delegate
[self.aCDMCameraCaptureNotificationDelegate imageDidSuccessfullyCaptured];
}];
图像,取决于它们的分辨率,可以在你的记忆中占据一点空间。你需要确保你正在制作的图像不会把所有东西都占满。有一个名为SDWebImage的web图像缓存库,它可以将图像存储在内存和磁盘中的键值存储中,因此,如果内存压力增加过多,它可以刷新内存中的图像缓存,当需要恢复图像时,可以从磁盘调用它
如果你在屏幕上显示图像,捕获的一个图像是很好的,但是如果你正在显示多个图像,考虑将图像缩小到相应的图像尺寸,并存储原始的高速缓存。
在分析之后,我看到下面的内存泄漏。
# Address Category Event Type RefCt Timestamp Size Responsible Library Responsible Caller
0 0x2087cb80 __NSDate Malloc 1 00:18.446.047 16 AVFoundation -[AVCaptureSession _stopPreviewing]
1 0x2087cb80 __NSDate Autorelease 00:18.446.056 0 AVFoundation -[AVCaptureSession _stopPreviewing]
2 0x2087cb80 __NSDate Release 0 00:28.472.781 0 Foundation -[NSAutoreleasePool drain]
3 0x2087cb80 __NSDate Free 0 00:28.472.787 -16 Foundation -[NSAutoreleasePool drain]
4 0x2087cb80 Malloc 16 Bytes Malloc 1 00:42.834.501 16 libdispatch.dylib _dispatch_call_block_and_release
您能指出是哪条语句导致了内存泄漏吗?除非您降低了捕获的图片的分辨率,否则您将无法减少这部分代码使用的内存量。你必须使你的应用程序准备好处理内存不足警告而不会崩溃。分析后,我发现以下内存泄漏。请执行,因为提交后的缩进显示不好。