iPhone UITableView异步加载图片

来源:互联网 发布:品茗网络计划 注册码 编辑:程序博客网 时间:2024/05/17 13:09

// Customize the appearance of table view cells.

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

    

    static NSString *CellIdentifier = @"Cell";

  

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier] autorelease];

}


[NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:selfwithObject:indexPath];



UIFont *font = [UIFont fontWithName:@"Helvetica" size:17];

//cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];

cell.textLabel.text = @"ok";

cell.textLabel.font = font;

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}


- (void)updateImageForCellAtIndexPath:(NSIndexPath *)indexPath{

    NSAutoreleasePool *pool = [[NSAutoreleasePool allocinit];

    UIImage *image = [self getImageForCellAtIndexPath:indexPath];

     if (image) {

            NSLog(@"image ok %d",indexPath.row);

     }

    UITableViewCell *cell = [self.tbView cellForRowAtIndexPath:indexPath];

CGRect rect;

UIImageView *iv = [[UIImageView allocinitWithFrame:CGRectMake(KNewCollectionImageViewX+5kTopButtonMargin,KShopCellImageViewWidthKShopCellImageViewHeight)];

iv.tag = KImageViewTag;

iv.backgroundColor = [UIColor clearColor];

rect = iv.frame;

//iv.image = image;

[cell addSubview:iv];

//cell.imageView.image = image;

    //[cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];

[iv performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];

[iv release];

    //[image release];

    [pool release];

}


-(UIImage *)getImageForCellAtIndexPath:(NSIndexPath *)indexPath{

   // id path = [[dataArray objectAtIndex:indexPath.row] objectForKey:@"image"];

//id path = [imageArray objectAtIndex:indexPath.row];

//id path = @"http://merrimusings.mu.nu/archives/images/groundhog2.jpg";

NSString *path = @"http://merrimusings.mu.nu/archives/images/groundhog2.jpg";


NSURL *url = [NSURL URLWithString:path];

NSData *data = [NSData dataWithContentsOfURL:url];

//UIImage *image = [[UIImage alloc] initWithData:data cache:NO];

   // NSURL *url = [NSURL URLWithString:path];

    //NSData *data = [NSData dataWithContentsOfURL:url];

    //UIImage *image = [[UIImage alloc] initWithData:data cache:NO];

//UIImage *image = [[UIImage alloc] initWithData:data cache:NO]; 


UIImage *image = [UIImage imageWithData:data];

if (image) {

NSLog(@"%@",path);

}


    return image;

}

原创粉丝点击