繁体   English   中英

如何消除uitableview单元中图像模糊的大量内存泄漏

[英]How to get rid of massive Memory leak with blurred image in uitableview cell

不知何故,当我滚动到表格的底部(仅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中查看,但这将显示我总共使用了内存堆,但是大的内存块并未出现在故障中。 奇怪的。

这就是我在左栏中使用内存堆的意思。 在此处输入图片说明

这是乐器中的分配位。 我看不出他们怎么能配对。 我什么都没有僵尸(除非在编辑方案中有其他地方可以改变)。 在此处输入图片说明

这是向下滚动后的泄漏仪表视图。 似乎没有显示任何实际的泄漏: 在此处输入图片说明

请参阅此链接中的解决方案。 问题在于图像的比例尺与屏幕比例尺不同。 最终的输出图像可能非常大。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM