【问题标题】:Low memory warning when using AVCaptureStillImageOutput使用 AVCaptureStillImageOutput 时内存不足警告
【发布时间】:2013-08-20 07:17:21
【问题描述】:

我在[self setImageData:[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]]; 观察到 25MB 的内存爆发

下面的代码 sn-p 正在捕获图片并提供 jpg 格式的数据。当我们拍摄更多照片时,系统会报告内存不足警告。

在分析器中我们看不到泄漏,但有时应用程序会报告内存不足警告,即使在 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];
 }];

【问题讨论】:

  • 除非您降低所捕获图片的分辨率,否则您将无法减少这部分代码使用的内存量。您必须让您的应用准备好处理内存不足警告而不会崩溃。
  • 分析后,我看到以下内存泄漏。

标签: iphone memory-management avcapturesession


【解决方案1】:

根据分辨率的不同,图像可能会占用您的内存中的一些空间。您需要确保您制作的图像不会占用所有内容。有一个名为 SDWebImage 的网络图像缓存库,它可以将您的图像存储在内存和磁盘上的键值存储中,因此如果内存压力增加太多,它可以刷新内存中的图像缓存,当您需要恢复图像时,您可以调用它来自磁盘。

如果您在屏幕上显示图像,则捕获的一张图像很好,但如果您显示多张图像,请考虑将图像的大小调整为相应的 imageView 大小并将原始图像存储在缓存中。

【讨论】:

    【解决方案2】:

    分析后,我看到以下内存泄漏。

    #   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
    

    你能指出哪个语句导致了这个内存泄漏吗?

    【讨论】:

    • 提交后缩进显示不好请执行
    猜你喜欢
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 2013-01-20
    • 2012-04-08
    • 1970-01-01
    相关资源
    最近更新 更多