Skip to content

Instantly share code, notes, and snippets.

@helloworld116
Created March 11, 2013 06:39
Show Gist options
  • Save helloworld116/5132250 to your computer and use it in GitHub Desktop.
Save helloworld116/5132250 to your computer and use it in GitHub Desktop.
ios:customcontrol:AsyncImageView图片异步加载
//控件下载地址:https://github.com/nicklockwood/AsyncImageView
//imageview本身有一张默认图片,远程图片请求完后,替换为远程图片
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//create new cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//common settings
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.frame = CGRectMake(0.0f, 0.0f, 44.0f, 44.0f);
cell.imageView.clipsToBounds = YES;
}
else
{
//cancel loading previous image for cell
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imageView];
}
//set placeholder image or cell won't update when image is loaded
cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
//load the image
cell.imageView.imageURL = [imageURLs objectAtIndex:indexPath.row];
//display image path
cell.textLabel.text = [[[imageURLs objectAtIndex:indexPath.row] path] lastPathComponent];
return cell;
}
//起初每个图片显示正在加载的状态,图片加载完毕则显示该图片
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
#define IMAGE_VIEW_TAG 99
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//create new cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//add AsyncImageView to cell
AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.0f)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
imageView.tag = IMAGE_VIEW_TAG;
[cell addSubview:imageView];
[imageView release];
//common settings
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.indentationWidth = 44.0f;
cell.indentationLevel = 1;
}
//get image view
AsyncImageView *imageView = (AsyncImageView *)[cell viewWithTag:IMAGE_VIEW_TAG];
//cancel loading previous image for cell
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView];
//load the image
imageView.imageURL = [imageURLs objectAtIndex:indexPath.row];
//display image path
cell.textLabel.text = [[[imageURLs objectAtIndex:indexPath.row] path] lastPathComponent];
return cell;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment