【问题标题】:How to get rid of massive Memory leak with blurred image in uitableview cell如何摆脱uitableview单元格中图像模糊的大量内存泄漏
【发布时间】:2013-11-02 02:15:40
【问题描述】:

不知何故,当我滚动到表格底部(仅限 96 个项目)时,我得到 1 GB 的内存使用量(它会随着创建的每个单元格而增加。我有一个表格,前面有一个模糊图像的图像它使用的是原始图像的裁剪版本,然后是文本。非常简单。我正在使用苹果提供的示例代码来模糊此处可用的图像:https://github.com/iGriever/TWSReleaseNotesView/blob/master/TWSReleaseNotesView/UIImage%2BImageEffects.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"itemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

NSDictionary *foodItem = [self.foodItems objectAtIndex:indexPath.row];

// Set up the image view
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
UIImage *foodImage = [UIImage imageNamed:[foodItem objectForKey:FOOD_IMAGE_FILE_KEY]];
[imageView setImage:foodImage];
[imageView setContentMode:UIViewContentModeScaleAspectFill];

// Set up the label
UILabel *labelView = (UILabel *)[cell viewWithTag:2];
[labelView setFont:[UIFont flatFontOfSize:20.0]];
labelView.text = @"Blah";

// Set up the image view
UIImageView *blurredView = (UIImageView *)[cell viewWithTag:3];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    UIImage *blurredImage = [[self cropForBlur:foodImage] applyBlurWithRadius:4
                                                                tintColor:[UIColor colorWithWhite:1.0 alpha:0.2]
                                                    saturationDeltaFactor:1.2
                                                                maskImage:nil];

    dispatch_sync(dispatch_get_main_queue(), ^{
        blurredView.image = blurredImage;
    });
});

return cell;

}

*注意:我知道这很可能是模糊(与我的裁剪相反),因为它仅在我进行模糊时发生。此外,这与异步调度无关,因为如果我不这样做,它仍然会发生。

是的,我正在使用 ARC。是的,我正在使用情节提要。

这里是cropForBlur:

- (UIImage *)cropForBlur:(UIImage *)originalImage
{
    CGSize size = [originalImage size];
    int startCroppingPosition = 100;
    if (size.height > size.width) {
        startCroppingPosition = size.height/2 + ((size.width / 320) * 45);
    } else {
        startCroppingPosition = size.height/2 + ((size.width / 320) * 45);
    }
    // WTF: Don't forget that the CGImageCreateWithImageInRect believes that
    // the image is 180 rotated, so x and y are inverted, same for height and width.
    CGRect cropRect = CGRectMake(0, startCroppingPosition, size.width, ((size.width/320) * 35));
    CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], cropRect);
    UIImage *newImage = [UIImage imageWithCGImage:imageRef scale:(size.width/160) orientation:originalImage.imageOrientation];
    CGImageRelease(imageRef);
    return newImage;
}

我也尝试过查看 Instruments,但它会显示我总共使用了大量内存,但大块内存并没有出现在故障中。很奇怪。

这就是说我在左侧栏中使用了大量内存。

这是 Instruments 中的分配位。我看不出他们怎么能匹配。我没有僵尸或任何东西(除非在编辑方案中有其他地方可以改变它)。

这是向下滚动一点后的泄漏仪器视图。似乎没有显示任何真正的泄漏:S 很困惑。

【问题讨论】:

  • 看起来不错,但信息不足,能否提供cropForBlue:实现?而你正在使用 ARC?
  • 你去。不过,我认为这与裁剪无关。
  • 所以如果你注释模糊创建代码你的应用程序不会消耗这么多内存?
  • 正确 :) 它保持在 20 mb 以下。
  • 您尝试过泄漏仪器吗?它将向您显示泄漏了哪些对象,然后显示了它创建的整个生命周期,以及谁保留和释放了它。

标签: ios objective-c cocoa-touch uitableview ios7


【解决方案1】:

请参阅此link 的解决方案。问题是图像的比例与您的屏幕比例不同。最终输出的图像可能非常大。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2022-07-30
    • 2020-09-22
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    相关资源
    最近更新 更多