在iPhone中延迟加载+无法显示下载的图像

时间:2012-05-28 11:01:18

标签: iphone objective-c ios lazy-loading

我正在为延迟加载实现一个示例代码。 我有一个阵列,其中有10个图像: - //自定义表格视图单元格的外观。

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];

    if (!iconDownloader.ImageDispaly)
    {
        if (Tble.dragging == NO && Tble.decelerating == NO)
        {
            [self startIconDownload:[ArrayImage objectAtIndex:indexPath.row] forIndexPath:indexPath];
        }

        //cell.imgView.image = [UIImage imageNamed:@"event_image.png"];
        cell.imageView.image=[UIImage imageNamed:@"default-image.png"];
        NSLog(@"image not download");
    }
    else
    {
        //cell.imgView.image=[aTweet objectForKey:@"image"];
        cell.imageView.image=iconDownloader.ImageDispaly.image;

    } 

    return cell;
}


#pragma mark -
#pragma mark Table cell image support
- (void)startIconDownload:(NSString *)str forIndexPath:(NSIndexPath *)indexPath
{
    //[imageDownloadsInProgress retain];
    iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];
    if (iconDownloader == nil) 
    {
        iconDownloader = [[IconDownloader alloc] init];
       // iconDownloader.appRecord = aTweet;
        iconDownloader.imageurlstring=[ArrayImage objectAtIndex:indexPath.row];
        iconDownloader.indexPathInTableView = indexPath;
        iconDownloader.delegate = self;
        [imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];
        [iconDownloader startDownload];
        [imageDownloadsInProgress retain];
        [iconDownloader release];   
    }
}

// this method is used in case the user scrolled into a set of cells that don't have their app icons yet
- (void)loadImagesForOnscreenRows
{
    if ([ArrayImage count] > 0)
    {
        NSArray *visiblePaths = [Tble indexPathsForVisibleRows];
        for (NSIndexPath *indexPath in visiblePaths)
        {
            iconDownloader =[imageDownloadsInProgress objectForKey:indexPath];

            if (!iconDownloader.ImageDispaly) // avoid the app icon download if the app already has an icon
            {
                [self startIconDownload:[ArrayImage objectAtIndex:indexPath.row] forIndexPath:indexPath];
            }
        }
    }
}

- (void)appImageDidLoad:(NSIndexPath *)indexPath
{
    iconDownloader =[imageDownloadsInProgress objectForKey:indexPath];
    if (iconDownloader != nil)
    {

        UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(10,5,60,60)];
        UITableViewCell *cell = [Tble cellForRowAtIndexPath:iconDownloader.indexPathInTableView];
        // [cell.btnImage setBackgroundImage:[iconDownloader.appRecord valueForKey:@"image"] forState:UIControlStateNormal];
        image.image=iconDownloader.ImageDispaly.image;
        cell.imageView.image=image.image;

        NSLog(@"%@ ======%@",cell.imageView.image,image.image);

    }

}

这就是我在控制台上获得的内容  NSLog(@“%@ ======%@”,cell.imageView.image,image.image); 在执行应用程序时

2012-05-28 16:41:44.998 Lazyloading[97195:207] <UIImage: 0x4b56300> ======<UIImage: 0x4b56300>
2012-05-28 16:41:44.999 Lazyloading[97195:207] <UIImage: 0x4e24f00> ======<UIImage: 0x4e24f00>
2012-05-28 16:41:45.000 Lazyloading[97195:207] <UIImage: 0x4b55df0> ======<UIImage: 0x4b55df0>
2012-05-28 16:41:45.001 Lazyloading[97195:207] <UIImage: 0x4b406e0> ======<UIImage: 0x4b406e0>
2012-05-28 16:41:45.260 Lazyloading[97195:207] <UIImage: 0x4e2f9f0> ======<UIImage: 0x4e2f9f0>
2012-05-28 16:41:45.263 Lazyloading[97195:207] <UIImage: 0x4e0a2e0> ======<UIImage: 0x4e0a2e0>
2012-05-28 16:41:45.294 Lazyloading[97195:207] <UIImage: 0x4b40180> ======<UIImage: 0x4b40180>

但是图片没有在tableview中显示.. 任何人都可以帮我解决这个问题..

1 个答案:

答案 0 :(得分:0)

尝试致电:

[cell setNeedsDisplay];

您的NSLog行是:

NSLog(@"%@ ======%@",cell.imageView.image,image.image);