简体   繁体   中英

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

Somehow when I scroll to the bottom of my table (96 items only) I get 1 gb of memory usage (it increase for every cell that gets created. I have a table that has an image with a blurred image in front of it that is using a cropped version of the original image with text then on top of that. Pretty simple. I'm using the apple provided sample code for blurring images available here: 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;

}

*Note: I know it is most likely the blur (as opposed to my cropping) as it only happens when I do the blur. Also it's nothing to do with the async dispatch stuff as it still happens if I don't do that.

Yes I'm using ARC. Yes I'm using storyboards.

Here's 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;
}

I've also tried looking in Instruments but it will show that I'm using heaps of memory in total but the big chunks of memory don't show up in the breakdown. Weird.

Here's the bit that's saying I am using heaps of memory in the left bar. 在此处输入图片说明

Here's the allocations bit in Instruments. I don't see how they could match up. I haven't got zombies on or anything (unless there's somewhere other that in edit scheme to change that). 在此处输入图片说明

Here's the leaks Instruments view after scrolling down a bit. Doesn't seem to show any real leaks :S So confused. 在此处输入图片说明

See the solution from this link . The problem is the image has a different scale from your screen scale. The final output image can be very big.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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