【问题标题】:App crash due to memory warning when calling "GPUImagePicture ProcessImage" repeatedly重复调用“GPUImagePicture ProcessImage”时出现内存警告导致应用程序崩溃
【发布时间】:2014-07-03 19:59:17
【问题描述】:

我已经实现了一个用于图像编辑的组过滤器(GPUImageSepiaFilter、GPUImageExposureFilter、GPUImageSepiaFilter)。我有一个用于设置自定义“曝光”(setExposure:) 值的滑块。在滑块的“didValueChanged”操作方法上,我通过调用“图片processImage”来刷新图像预览。如果我快速移动滑块或反复滚动滑块时,应用程序肯定会由于内存问题而崩溃。

- (void)viewDidLoad  {
    [super viewDidLoad];

    self.originalPicture = [[GPUImagePicture alloc] initWithImage:[UIImage imageNamed:@"IMG_0009.JPG"]];

    self.filterGroup = [[GPUImageFilterGroup alloc] init];

    GPUImageSepiaFilter *sepiaFilter = [[GPUImageSepiaFilter alloc] init];
    [self.filterGroup addFilter:sepiaFilter];

    GPUImageExposureFilter *pixellateFilter = [[GPUImageExposureFilter alloc] init];
    [pixellateFilter setExposure:0.0f];
    [self.filterGroup addFilter:pixellateFilter];

    GPUImageSaturationFilter *saturation = [[GPUImageSaturationFilter alloc] init];
    [self.filterGroup addFilter:saturation];

    [sepiaFilter addTarget:pixellateFilter];
    [pixellateFilter addTarget:saturation];

    [self.filterGroup setInitialFilters:[NSArray arrayWithObjects:sepiaFilter, pixellateFilter,nil]];
    [self.filterGroup setTerminalFilter:saturation];

    [self.originalPicture addTarget:self.filterGroup];


    GPUImageView *filterView = [[GPUImageView alloc] init];
    self.view = filterView;

    [self.filterGroup addTarget:filterView];

    [self.originalPicture processImage];

    [self.slider setMinimumTrackTintColor:[UIColor redColor]];
    [self.slider setMaximumTrackTintColor:[UIColor greenColor]];
    [self.view addSubview:self.slider]; 
}

- (IBAction)didChangeValue:(id)sender {
    GPUImageExposureFilter *filter = (GPUImageExposureFilter *)[self.filterGroup filterAtIndex:1];

    [filter setExposure:self.slider.value];
    [self.originalPicture processImage];    
}

解决此问题的最佳方法是什么?还是我做错了什么?

谢谢,
斯里尼瓦斯

【问题讨论】:

  • 图片的像素大小?
  • @Andrea:图像大小应为 743KB 到 2.5MB。
  • 以像素为单位的大小,而不是字节... ;-) 它可以在图像处理中产生很大的不同。在内存中解压的图像是 heightwidthn°channel*n°bit_for_each_channel
  • @Andrea,很抱歉。它的大小为 2000x1333。
  • @Srinivas - 每个帧缓冲区有 10.7 MB 的未压缩内存(其中至少有两个)。尝试在您的第一个过滤器上使用-forceProcessingAtSize: 以将其大小减小到目标视图的像素大小。这将显着减少内存消耗,并且还将真正加快此处的处理速度。

标签: ios memory-management gpuimage gpuimagestillcamera


【解决方案1】:

我只是关于图像和 GPUImage 的一些建议:

  • “磁盘”大小的图像并不代表真实的图像大小,这是因为它可能已被压缩。实际图像大小,当它在内存中解压缩并且系统必须处理它时:height*width*n°channel*n°bit_for_each_channel
  • 图像应该总是延迟加载,如果你不使用它们就没有用了
  • Brad 在其关于帧缓冲区重用的框架中进行了巨大更改,对内存的处理方式进行了重大改进,您确定您使用的是 github 上的最新版本吗?
  • 您是否尝试过使用分配工具分析应用程序,也许问题出在其他地方,使用此工具您可以查看内存是否增长到您期望的位置
  • imageNamed 方法缓存图像,即使他们说在内存压力情况下该内存将被驱逐,我也从未有机会看到清除工作

我在使用 GPUImage 时没有发现您的代码有任何问题,但我会尝试使用较小的图像(以像素大小为单位)并首先使用分配。

【讨论】:

  • 安德烈亚,感谢您的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多