表视图与异步图像加载行为怪异

时间:2014-02-10 05:35:49

标签: ios

我的桌面视图有背景视图,上面有一些文字。我正在从网址下载背景(在下面的代码中它是硬编码的,但我将为每个单元格提供不同的网址。为了改善响应,我使用图像缓存来保存下载的图像并异步加载图像。我我正在为图像使用淡入淡出的动画。除了一些奇怪的行为外,一切都很好。第一次,图像加载很好。如果我滚动,图像会调整到每个单元格中的其他内容。如果我停止滚动,我可以看到图像在每个单元格中随机重新加载。有时图像会消失并在几秒钟后重新出现。

我在iOS上做得不够好,请帮忙。

这是我的单元格数据源方法:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==0){
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
        [cell.contentView addSubview:self.headerFrame];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return  cell;
    }
    static NSString *cellIdentifier = @"HistoryCell";
    // Similar to UITableViewCell, but
    CustomSaloonCell *cell = (CustomSaloonCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[CustomSaloonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NSString *identifier = [NSString stringWithFormat:@"Cell%ld" ,
                            (long)indexPath.row];
    if([self.cachedImages objectForKey:identifier] != nil){
      cell.backgroundView = [[UIImageView alloc]initWithImage:[cachedImages valueForKey:identifier]];
    } 
    else {
        char const * s = [identifier  UTF8String];
        dispatch_queue_t queue = dispatch_queue_create(s, 0);
        dispatch_async(queue, ^{
            NSURL *url = [NSURL URLWithString:@"http://upload.wikimedia.org/wikipedia/en/thumb/7/73/Shrekcharacter.jpg/220px-Shrekcharacter.jpg"];
            UIImage *img = nil;
            NSData *data = [[NSData alloc] initWithContentsOfURL:url];
            img = [[UIImage alloc] initWithData:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([theTableView indexPathForCell:cell].row == indexPath.row) {
                    [cachedImages setValue:img forKey:identifier];
                    cell.backgroundView = [[UIImageView alloc]initWithImage:[cachedImages valueForKey:identifier]];
                    cell.backgroundView.alpha = 0.0;
                    [UIView animateWithDuration:5.0 animations:^{
                        cell.backgroundView.alpha = 1.0;
                    }];
                }
            });
        });
    }
    /********/
    cell.stylistName.text = [salonDetailsArray objectAtIndex:indexPath.row][@"salonName"];
    cell.salonAddress.text = [salonDetailsArray objectAtIndex:indexPath.row][@"salonAddress"];
    cell.delegateListener = self;
    cell.claimButton.frame =CGRectMake(494/2, 216/2, 120/2, 46.0/2.0);
    [cell.claimButton addTarget:self action:@selector(claimClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.claimButton setTitle:@"claim" forState:UIControlStateNormal];
    if([loginStatus isEqualToString:@"YES"]){
        [cell.claimButton setTitleColor:[UIColor colorWithRed:106.0/255.0 green:45.0/255.0 blue:118.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    }else {

         [cell.claimButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }

    cell.claimButton.tag = indexPath.row;
    cell.overlay.hidden = YES;
    for (NSIndexPath *selectedIndex in self.buttonStateArray){
        if([selectedIndex isEqual:indexPath]){
            [cell.claimButton setTitle:@"claimed" forState:UIControlStateNormal];
               cell.claimButton.frame = CGRectMake(464/2-(30/2), 216/2, 180/2, 46.0/2.0);
            cell.overlay.hidden = NO;
            [cell.claimButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    return cell;
}

2 个答案:

答案 0 :(得分:0)

使用自己的代码下载和缓存图像很方便,但不推荐使用b'cos,有许多高度优化的类可以非常巧妙地使用它。 我更喜欢为业务逻辑编写自己的代码,并且使用优化的库来实现性能是有帮助的。

https://github.com/rs/SDWebImage

“SDWebImage”满足您的要求,支持缓存,异步图像下载,完成块也可以在那里制作动画内容。

答案 1 :(得分:0)

另请尝试SmartStorage。它会自动将UIImage / NSData缓存在磁盘或内存中。

这是GitHub的解释图:

enter image description here

也有代码示例。 祝你好运。

相关问题