1

我正在使用 NSOperationQueue 在后台缓存图像。下面是代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell;
cell = [self.mUpcomEventsTable dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

UIImageView *imgView;
UILabel *lblEventName;
UILabel *lblDate;
UILabel *lblTime;
if(self.mEventNameArr != NULL)
{
NSString *imageUrlString = [NSString stringWithFormat:@"%@", [self.mEventImageArr objectAtIndex:indexPath.row]];
UIImage *cachedImage = [self.imageCache objectForKey:imageUrlString];
    NSLog(@"cache:%@", self.imageCache);

    imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 90)];

    lblEventName = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 200, 30)];
    lblEventName.text = [self.mEventNameArr objectAtIndex: indexPath.row];
    lblEventName.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    lblDate = [[UILabel alloc]initWithFrame:CGRectMake(90, 50, 100, 30)];
    lblDate.text = [self.mEventDateArr objectAtIndex:indexPath.row];
    lblTime = [[UILabel alloc]initWithFrame:CGRectMake(190, 50, 100, 30)];
    lblTime.text = [self.mEventTimeArr objectAtIndex:indexPath.row];

    strEventName = lblEventName.text;
    strEventDate = lblDate.text;
    strEventTime = lblTime.text;

if (cachedImage)
{
    imgView.image = cachedImage;
}
else
{
    // you'll want to initialize the image with some blank image as a placeholder

    imgView.image = [UIImage imageNamed:@"Placeholder.png"];

    // now download in the image in the background
    NSLog(@"queue:%@",self.imageDownloadingQueue);
    [self.imageDownloadingQueue addOperationWithBlock:^{

        NSURL *imageUrl   = [NSURL URLWithString:imageUrlString];
        NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
        UIImage *image    = nil;
        if (imageData)
            image = [UIImage imageWithData:imageData];

        if (image)
        {

            [self.imageCache setObject:image forKey:imageUrlString];

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                UITableViewCell *updateCell = [tableView cellForRowAtIndexPath:indexPath];
                if (updateCell)
                    imgView.image = cachedImage;
            }];
        }
    }];
}
}
else cell.textLabel.text = @"No event";
[cell addSubview:imgView];
[cell addSubview:lblEventName];
[cell addSubview:lblDate];
[cell addSubview:lblTime];
return cell;
}

它不会进入这一行 [self.imageDownloadingQueue addOperationWithBlock:^{ 它会从这里出去。为什么这样,请帮助上面。从这里获取想法链接

4

1 回答 1

0

使用有很多缺点NSOperationQueue。其中之一是每次滚动时您的图像都会闪烁UITableView

我建议你使用这个AsyncImageView。我用过它,效果很好。要调用此 API:

ASyncImage *img_EventImag = alloc with frame;
NSURL *url = yourPhotoPath;
[img_EventImage loadImageFromURL:photoPath];
[self.view addSubView:img_EventImage]; // In your case you'll add in your TableViewCell.

这与使用 UIImageView 相同。很简单,它可以为您完成大部分工作。AsyncImageView 包括 UIImageView 上的一个简单类别,用于在 iOS 上异步加载和显示图像,以便它们不会锁定 UI,以及用于更高级功能的 UIImageView 子类。AsyncImageView 与 URL 一起使用,因此它可以与本地或远程文件一起使用。

加载/下载的图像缓存在内存中,并在出现内存警告时自动清理。AsyncImageView 独立于 UIImage 缓存运行,但默认情况下,位于应用程序包根目录中的任何图像都将存储在 UIImage 缓存中,以避免缓存图像的任何重复。

该库还可用于独立于 UIImageView 加载和缓存图像,因为它提供对底层加载和缓存类的直接访问。

于 2013-03-28T06:17:38.257 回答