简体   繁体   中英

iOS7 tableview custom cell image memory

i am using a custom cell (CustomCell) with a subview (ViewDrawing) to draw an image. Later on i want to draw more. The cells are also different in height.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    float cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];

    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [cell.imageView setFrame:CGRectMake(0, 0, cell.frame.size.width, cellHeight)];
    if (indexPath.row < 10) {
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"FICDDemoImage00%ld.jpg",indexPath.row]];
    } else {
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"FICDDemoImage0%ld.jpg",indexPath.row]];
    }

    [cell.imageView setNeedsDisplay];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 20 + indexPath.row * 2;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //rought calculation
    return 20 + indexPath.row * 2;
    //return UITableViewAutomaticDimension;
}

CustomCell.m (view is of type ViewDrawing. its an class variable)

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        view = [[ViewDrawing alloc] init];
        [self addSubview:view];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (ViewDrawing *)viewDrawing {
    return view;
}

- (void)setViewDrawing:(ViewDrawing *)viewDrawing {
    view = viewDrawing;
}

And my drawing method in ViewDrawing.m looks like that:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextFillRect(context, rect);

    [image drawInRect:rect];
}

The problem is now everything works fine but the scrolling is sometimes not very fluid. Also the app uses 300mb memory which is way to much! It seems like every cell is held in memory and not released. Anyone ideas how to reduce the problems?

EDIT:

I changed the drawRect to:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.Vendor.Draw",NULL);
    dispatch_async(backgroundQueue, ^{
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
        CGContextFillRect(context, rect);

        [image drawInRect:rect];
    });

}

and i now use imageWithContentsOfFile. Now it uses 50mb which is ok i think?!

First: using [image drawInRect:] in drawRect is rather expensive on the mainthread, this should be the reason why your scrollview feels slugish.

Second: using [UIImage imageNamed:] keeps the images in memory and should be the reason why your memory consumption rises. If you use [UIImage imageWithContentsOfFile:] your memory footprint should drop, but your scrolling performance might even become worse.

to handle a large numer of big images you should try to load and draw your cells in a background thread and fade them in when rendering is finished.

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