uitableview单元中的异步图像加载 [英] Asyncronous image loading in uitableview cell
问题描述
我是iPhone编程的新手.我想将图像加载到UITableView中.我的图片来自网络.我知道我必须使用 ASIHttp Request
,然后将图像加载到UITableview.但是,如果我这样做,则直到其加载图像UITableview挂起.如果我使用异步图像加载块,则每次 Cell == nil
或重用请求女佣时,我都不想这样做.
I am new in iPhone programming. I want to load images in to the UITableView. My images came from the web. I know that I must use ASIHttp Request
and then load images to the UITableview. But if I do that then until its load images UITableview hang.
If I use asynchronous image loading block then each time when Cell==nil
or Reuse the request maid, that I doesn't want to do.
以下是我在Tableview中使用的代码行 cellForRowAtIndexpath
.
Following line of code I am using in Tableview cellForRowAtIndexpath
.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImageView *imgViewCellBg;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType=UITableViewCellAccessoryNone;
imgViewCellBg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,151)];
imgViewCellBg.userInteractionEnabled = NO;
imgViewCellBg.backgroundColor = [UIColor whiteColor];
imgViewCellBg.tag = 10000;
[cell.contentView addSubview:imgViewCellBg];
[imgViewCellBg release];
}
else{
imgViewCellBg = (UIImageView *)[cell.contentView viewWithTag:10000];
}
//-------------------------------- For the image Downloding And Displaying ------
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:(MY Url)]];
[request setCompletionBlock:^{
// NSLog(@"Image downloaded.");
NSData *data = [request responseData];
UIImage *imageCountry = [[UIImage alloc] initWithData:data];
[imgViewCellBg setImage:imageCountry];
imageCountry = Nil;
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"Error downloading image: %@", error.localizedDescription);
}];
[request startAsynchronous];
//-----------------------------------------------------------------
return cell;
推荐答案
This is an example with AysncImageView
, you can download this class from AynchImageView with Example
您可以像下面的代码一样访问此类.
You can access this class like bellow code.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ImageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]
autorelease];
} else {
AsyncImageView* oldImage = (AsyncImageView*)
[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}
CGRect frame;
frame.size.width=75; frame.size.height=75;
frame.origin.x=0; frame.origin.y=0;
AsyncImageView* asyncImage = [[[AsyncImageView alloc]
initWithFrame:frame] autorelease];
asyncImage.tag = 999;
NSURL* url = [imageDownload
thumbnailURLAtIndex:indexPath.row];
[asyncImage loadImageFromURL:url];
[cell.contentView addSubview:asyncImage];
return cell;
}
希望这对您有所帮助.
这篇关于uitableview单元中的异步图像加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!