GPUImage内存累积过滤图像到UITableViewCell的UIImageView

时间:2014-08-18 22:40:09

标签: ios objective-c xcode uitableview gpuimage

我已将一堆GPUImage过滤器列入UITableView,每个UITableViewCell有一个过滤器。我正在使用UITableViewCell的UIImageView图像来显示该过滤器图像的样本。

但是,当弹出列出过滤器的视图时,不会清除内存。当我再次打开女性时,它会积累更多未被释放的记忆。挂断的地方在哪里可以不释放内存?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger index = [indexPath row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FilterCell"];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FilterCell"];
        cell.textLabel.textColor = [UIColor blackColor];
    }       

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    switch( index )
    {
        case GPUIMAGE_SATURATION: cell.textLabel.text = @"Saturation"; break;
        case GPUIMAGE_CONTRAST: cell.textLabel.text = @"Contrast"; break;
        case GPUIMAGE_BRIGHTNESS: cell.textLabel.text = @"Brightness"; break;
        case GPUIMAGE_LEVELS: cell.textLabel.text = @"Levels"; break;
        case GPUIMAGE_EXPOSURE: cell.textLabel.text = @"Exposure"; break;
        case GPUIMAGE_RGB: cell.textLabel.text = @"RGB"; break;
        case GPUIMAGE_HUE: cell.textLabel.text = @"Hue"; break;
        case GPUIMAGE_WHITEBALANCE: cell.textLabel.text = @"White balance"; break;
        case GPUIMAGE_MONOCHROME: cell.textLabel.text = @"Monochrome"; break;
        /* More omitted */
    }

    NSString *identifier = [NSString stringWithFormat:@"%lu", (unsigned long)index];

    UIImage *image = imageCache[ identifier ];
    if( image )
       [[cell imageView] setImage:image];
    else
    {
        // Placeholder image
        [[cell imageView] setImage:[UIImage imageNamed:@"Placeholder.png"]];

//        if( myTableView.dragging == NO && myTableView.decelerating == NO)
//        {
            dispatch_async(dispatch_get_main_queue(), ^{
                UIImage *image = [self filterPreview:index];

                if( !image )
                    return;

                imageCache[ identifier ] = image;

                // Make sure the cell is still available
                NSIndexPath *myIndexPath = [myTableView indexPathForCell:cell];
                if( myIndexPath==nil || myIndexPath.row==indexPath.row )
                {
                    [[cell imageView] setImage:image];
                    [myTableView beginUpdates];
                    [myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                    [myTableView endUpdates];
                }
            });
//        }
    }

    return( cell );
}

函数调用:

- (UIImage *)filterPreview:(GPUImageShowcaseFilterType)filterType
{
    GPUImageOutput<GPUImageInput> *filter;

    switch( filterType )
    {
        case GPUIMAGE_SEPIA:
        {
            filter = [[GPUImageSepiaFilter alloc] init];
        }; break;
        case GPUIMAGE_PIXELLATE:
        {
            filter = [[GPUImagePixellateFilter alloc] init];
        }; break;
        case GPUIMAGE_POLARPIXELLATE:
        {
            filter = [[GPUImagePolarPixellateFilter alloc] init];
        }; break;
        case GPUIMAGE_PIXELLATE_POSITION:
        {
            filter = [[GPUImagePixellatePositionFilter alloc] init];
        }; break;
        case GPUIMAGE_POLKADOT:
        {
            filter = [[GPUImagePolkaDotFilter alloc] init];
        }; break;
        case GPUIMAGE_HALFTONE:
        {
            filter = [[GPUImageHalftoneFilter alloc] init];
        }; break;
        case GPUIMAGE_CROSSHATCH:
        {
            filter = [[GPUImageCrosshatchFilter alloc] init];
        }; break;
        case GPUIMAGE_COLORINVERT:
        {
            filter = [[GPUImageColorInvertFilter alloc] init];
        }; break;
        case GPUIMAGE_GRAYSCALE:
        {
            filter = [[GPUImageGrayscaleFilter alloc] init];
        }; break;
        case GPUIMAGE_MONOCHROME:
        {
            filter = [[GPUImageMonochromeFilter alloc] init];
            [(GPUImageMonochromeFilter *)filter setColor:(GPUVector4){0.0f, 0.0f, 1.0f, 1.f}];
        }; break;
        /* More of the same omitted */
    }

    UIImage *returnImage = [filter imageByFilteringImage:sampleImage];
    return( returnImage );
}

0 个答案:

没有答案