[英]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.