gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:53:30 25 4
gpt4 key购买 nike

我正在使用带有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com