简体   繁体   English

iOS7 tableview自定义单元格图像内存

[英]iOS7 tableview custom cell image memory

i am using a custom cell (CustomCell) with a subview (ViewDrawing) to draw an image. 我正在使用带有子视图(ViewDrawing)的自定义单元格(CustomCell)来绘制图像。 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) CustomCell.m(视图的类型为ViewDrawing。它是一个类变量)

- (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: 我在ViewDrawing.m中的绘制方法如下所示:

- (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! 该应用程序还使用300mb内存,这是很多方法! 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: 我将drawRect更改为:

- (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. 我现在使用imageWithContentsOfFile。 Now it uses 50mb which is ok i think?! 现在它使用50mb,我认为可以吗?

First: using [image drawInRect:] in drawRect is rather expensive on the mainthread, this should be the reason why your scrollview feels slugish. 首先:在主线程上使用drawRect [image drawInRect:]相当昂贵,这应该是您的滚动视图感觉迟钝的原因。

Second: using [UIImage imageNamed:] keeps the images in memory and should be the reason why your memory consumption rises. 第二:使用[UIImage imageNamed:]将图像保留在内存中,这应该是内存消耗增加的原因。 If you use [UIImage imageWithContentsOfFile:] your memory footprint should drop, but your scrolling performance might even become worse. 如果使用[UIImage imageWithContentsOfFile:]内存占用量应该减少,但是滚动性能甚至会变差。

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. 要处理大量的大图像,您应该尝试在背景线程中加载并绘制单元格,并在渲染完成后淡入它们。

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

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