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

标签 ios memory tableview

我正在使用带有 subview (ViewDrawing) 的自定义单元格 (CustomCell) 来绘制图像。以后想多画点。细胞的高度也不同。

- (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 是 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;
}

我在 ViewDrawing.m 中的绘图方法如下所示:

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

    [image drawInRect:rect];
}

问题是现在一切正常,但滚动有时不是很流畅。此外,该应用程序使用 300mb 内存,这已经够多了!似乎每个单元格都保存在内存中而不被释放。有人知道如何减少问题吗?

编辑:

我将 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];
    });

}

我现在使用 imageWithContentsOfFile。现在它使用 50mb,我认为这可以吗?!

最佳答案

首先:在drawRect中使用[image drawInRect:]在主线程上是相当昂贵的,这应该是你的scrollview感觉迟钝的原因。

第二:使用[UIImage imageNamed:] 将图像保存在内存中,这应该是内存消耗增加的原因。如果您使用 [UIImage imageWithContentsOfFile:],您的内存占用应该会下降,但您的滚动性能甚至可能会变得更差。

要处理大量大图像,您应该尝试在后台线程中加载和绘制单元格,并在渲染完成时淡入。

关于iOS7 tableview自定义单元格图像内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24208385/

相关文章:

css - JavaFX TableView 更改选定的单元格颜色

c - char 和整数数组之间的速度差异?

c++ - 如何在 Linux(和 OSX)上查询分配的内存量?

ios - 我在他的类中设置 UITableViewCell 颜色后,它的颜色不再保持不变

ios - Swift 中的循环振动

ios - Xcode 上的 Swift._ArrayBuffer._copyContents

ios - 如何从单元测试类访问静态变量?

ios - 从第一个 UIAlertController 打开第二个 UIAlertController

c - 我自己的 malloc() 函数中的指针算术问题

tableview - Cappuccino V Sproutcore-Tableview