簡體   English   中英

iOS7 tableview自定義單元格圖像內存

[英]iOS7 tableview custom cell image memory

我正在使用帶有子視圖(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(視圖的類型為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:]相當昂貴,這應該是您的滾動視圖感覺遲鈍的原因。

第二:使用[UIImage imageNamed:]將圖像保留在內存中,這應該是內存消耗增加的原因。 如果使用[UIImage imageWithContentsOfFile:]內存占用量應該減少,但是滾動性能甚至會變差。

要處理大量的大圖像,您應該嘗試在背景線程中加載並繪制單元格,並在渲染完成后淡入它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM