使用UIImages加速UITableView作为detailView中的背景和UIImages

时间:2011-10-16 18:10:11

标签: iphone objective-c uitableview uiimage

我正在创建一个库存应用程序并且已经将一个分组UITableView子类化,我将后台设置为UIImage,我在初始化后根据位置(顶部,中间,底部,单个)设置。然后我根据缓存的类别和工具数组设置细节图像和文本。问题是,当压力测试(或根本测试)时,这种情况非常缓慢。任何人都可以告诉我为什么这个运行缓慢和/或给我一些指示,让它更快?

这应该是这样的:

tableview in quetion

这是子类UITableViewCell

的代码
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.backgroundImage = [[[UIImageView alloc] initWithFrame:CGRectMake(-3, 0, 305.0f, 50)] autorelease];
        self.roundedImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 33, 33)] autorelease];
        self.label = [[[UILabel alloc] initWithFrame:CGRectMake(50, 7, 140, 40)] autorelease];
        self.topDes = [[[UILabel alloc] initWithFrame:CGRectMake(215, 5, 100, 15)] autorelease];
        self.botDes = [[[UILabel alloc] initWithFrame:CGRectMake(215, 18, 100, 35)] autorelease];
        self.midDes = [[[UILabel alloc] initWithFrame:CGRectMake(215.0f, 15.0f, 80.0f, 22.0f)] autorelease];
        [self.contentView addSubview:self.backgroundImage];
        [self.contentView addSubview:self.roundedImageView];
        [self.contentView addSubview:self.label];
        [self.contentView addSubview:self.topDes];
        [self.contentView addSubview:self.midDes];
        [self.contentView addSubview:self.botDes];

        //Set properties for labels and images

        [self prepareForReuse];

    }
    return self;
}

以下是为tableview制作单元格的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (searching == NO) {

        NSString *CellIdentifier = [NSString stringWithFormat:@"%d, %d", indexPath.section, indexPath.row];
        OBcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            cell = [[[OBcell alloc]
                     initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]
                    autorelease];
            NSString *CellIdentifier = [NSString stringWithFormat:@"%d, %d", indexPath.section, indexPath.row];
        OBcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            cell = [[[OBcell alloc]
                     initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]
                    autorelease];
            if (searching == NO) {
                if (indexPath.row == 0) {
                    if ([tableView numberOfRowsInSection:indexPath.section] == 1) {
                        cell.backgroundImage.image = singleTableImage;
                    } else {
                        cell.backgroundImage.image = topTableImage;
                    }
                } else if (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) {
                    cell.backgroundImage.image = middleTableImage;
                } else {
                    cell.backgroundImage.image = bottomTableImage;
                }
                if (type == OBViewTypeTools) {
                    if ([[[[primaryArray objectAtIndex:indexPath.section] objectAtIndex:5] objectAtIndex:indexPath.row] integerValue] > 0) {
                        if (indexPath.row == 0) {
                            if ([tableView numberOfRowsInSection:indexPath.section] == 1) {
                                cell.backgroundImage.image = singleTableImageRed;
                            } else {
                                cell.backgroundImage.image = topTableImageRed;
                            }
                        } else if (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) {
                            cell.backgroundImage.image = middleTableImageRed;
                        } else {
                            cell.backgroundImage.image = bottomTableImageRed;
                        }
                    }
                }
            }
        }

            //Just pay attention to the cell.roundedImageView mainly, I'm thinking that's the problem unless you guys think it's something else...

        switch (type) {
            case OBViewTypeTools:;
                cell.label.text = (NSString*)[[[primaryArray objectAtIndex:indexPath.section] objectAtIndex:2] objectAtIndex:indexPath.row];
                cell.roundedImageView.image = [[[primaryArray objectAtIndex:indexPath.section] objectAtIndex:1] objectAtIndex:indexPath.row];
                cell.topDes.text = [NSString stringWithFormat:@"Total: %d", [[[[primaryArray objectAtIndex:indexPath.section] objectAtIndex:3] objectAtIndex:indexPath.row] integerValue]];
                cell.topDes.textColor = [UIColor yellowColor];
                cell.topDes.font = smallFont;
                cell.botDes.text = [NSString stringWithFormat:@"In Use: %d", [[[[primaryArray objectAtIndex:indexPath.section] objectAtIndex:4] objectAtIndex:indexPath.row] integerValue]];
                cell.botDes.textColor = [UIColor blueColor];
                cell.botDes.font = smallFont;
                break;
            case OBViewTypeCategories:;
                ToolCategory *cat = (ToolCategory*)[primaryArray objectAtIndex:indexPath.row];
                cell.label.text = cat.name;
                cell.midDes.textColor = [UIColor lightGrayColor];
                cell.midDes.font = smallFont;
                cell.midDes.text = [NSString stringWithFormat:@"Tools: %d", [[cat tools] count] == 0 ? 0 : [[cat tools] count]];
                cell.roundedImageView.image = cat.image;
                break;
            case OBViewTypeJobs:
                cell.label.text = ((Job*)[primaryArray objectAtIndex:indexPath.row]).name;
                cell.roundedImageView.image = ((Job*)[primaryArray objectAtIndex:indexPath.row]).image;
                cell.midDes.text = [NSString stringWithFormat:@"Tools: %d", [((Job*)[primaryArray objectAtIndex:indexPath.row]).tools count]];
                cell.midDes.font = smallFont;
                cell.midDes.textColor = [UIColor lightGrayColor];
                break;
            case OBViewTypeJobTools:
                cell.label.text = [((Tool*)[[[((ToolCategory*)[primaryArray objectAtIndex:indexPath.section]) tools] sortedArrayUsingDescriptors:sortDescriptors] objectAtIndex:indexPath.row]) name];
                cell.roundedImageView.image = [((Tool*)[[[((ToolCategory*)[primaryArray objectAtIndex:indexPath.section]) tools] sortedArrayUsingDescriptors:sortDescriptors] objectAtIndex:indexPath.row]) image];
                cell.midDes.text = [NSString stringWithFormat:@"(%d)", [[((ToolCategory*)[primaryArray objectAtIndex:indexPath.section]) tools] count]];
                cell.midDes.font = smallFont;
            case OBViewTypeToolJobs:
                cell.label.text = [((Job*)[secondaryArray objectAtIndex:indexPath.row]) name];
                cell.roundedImageView.image = [((Job*)[secondaryArray objectAtIndex:indexPath.row]) image];
                cell.midDes.textColor = [UIColor blueColor];
                cell.midDes.text = [NSString stringWithFormat:@"(%d)", [((Job*)[secondaryArray objectAtIndex:indexPath.row]) amountForTool:(Tool*)primaryObject]];
                cell.midDes.font = smallFont;
                break;
        }
        cell.clipsToBounds = NO;
        cell.layer.masksToBounds = NO;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
}

1 个答案:

答案 0 :(得分:1)

您的设置存在许多潜在问题,包括:

1)您的小区重用标识符非常奇怪

NSString *CellIdentifier = [NSString stringWithFormat:@"%d, %d",   
                                            indexPath.section, indexPath.row];

单元重用标识符应该是一个静态NSString,用于像细胞一样使用deque。不重复使用单元是非常昂贵的,因为必须实例化新单元而不是重用。查看示例屏幕截图,所有这些单元格实际上都是相同的,因此您应该使用相同的重用标识符:

static NSString *CellIdentifier = @"InventoryItemCell";

2)你在细胞中使用了很多alpha透明度。在Core Animation中,它需要额外的处理能力来混合alpha通道,并且应尽可能地减少透明度,尤其是在UITableView中。您可以通过启用“颜色混合图层”调试选项来运行“核心动画工具”,以了解应用中使用的Alpha数量:

enter image description here

这将叠加显示您的应用的哪些部分进行颜色混合。绿色是不透明的,红色是alpha:

enter image description here

这样做的目的是在动画发生时尽可能减少屏幕上的红色(坏!)。有时,由于设计要求,并非所有这些都可以消除,但尽可能减少它会产生良好的效果。

3)我不确定你是如何在你的图像上完成圆角的,但如果你是通过在UIImageView的CALayer上四处转角来制作它,那么在桌面视图中动画是非常昂贵的。如果您在代码中执行此操作,请不要这样做。使用已经舍入的图像来保存图像更加高效。 (但再次认识到阿尔法混合带来的惩罚!)