Iphone 表视图中的图像延迟加载
我想在我的表视图中使用延迟加载图像的概念。我的图像规则字符串数组是(Iphone 表视图中的图像延迟加载,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我想在我的表视图中使用延迟加载图像的概念。我的图像规则字符串数组是(MSMutableArray)“图像”。如何在cellforrowatinexpath中实现此功能 它看起来像: -(UITableViewCell *) tableView:(UITableView *)AtableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell=[searchtable dequeueReusab
MSMutableArray
)“图像”。如何在cellforrowatinexpath
中实现此功能
它看起来像:
-(UITableViewCell *) tableView:(UITableView *)AtableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[searchtable dequeueReusableCellWithIdentifier:@"cell"];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.textLabel.font=[UIFont systemFontOfSize:12];
cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[appSmallLogos objectAtIndex:indexPath.row]]]];
cell.detailTextLabel.text=[appReleaseDates objectAtIndex:indexPath.row];
}
return cell;
}
您可以使用该库,该库将异步下载和缓存映像。通过将url作为参数传递给它
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.textLabel.text = @"My Text";
return cell;
}
请尝试以下代码:
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.frame = CGRectMake(your bounds);
[cell addSubview:indicator];
[indicator bringSubviewToFront:self.view];
[indicator startAnimating];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
//This is what you will load lazily
NSString *u=[NSString stringWithContentsOfFile:r.url encoding:NSUTF8StringEncoding error:nil];
NSURL *imageURL=url;
NSData *image=[NSData dataWithContentsOfURL:imageURL];
dispatch_sync(dispatch_get_main_queue(), ^{
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image=[UIImage imageWithData:image];
[cell setNeedsLayout];
[indicator stopAnimating];
});
});
cell.cellDescription.text=r.url;// this is which you want to add in first time.
我不确定,但试试看
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
TableCellLayout *cell = (TableCellLayout *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[TableCellLayout alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.cellName = [[array valueForKey:@"name"] objectAtIndex:indexPath.row];
cell.cellImage = [UIImage imageNamed:@"placeholder.png"]; // add a placeholder
NSString *imageURL = [[array valueForKey:@"imageLink"] objectAtIndex:indexPath.row];
NSURL *theURL = [NSURL URLWithString:imageURL];
if (asynchronousImageLoader == nil){
asynchronousImageLoader = [[AsynchronousImages alloc] init];
}
[asynchronousImageLoader loadImageFromURL:theURL];
cell.cellImage = asynchronousImageLoader.image;
return cell;
}
我终于成功地通过以下方式设置了图像:
– performSelectorOnMainThread:withObject:waitUntilDone:
下载图像后[cell.imageView setImageWithURL:[NSURL URLWithString:@”占位符图像:[UIImage imagename:@“placeholder.png”]];此行显示setImageWithUrl和占位符图像的错误。请确保已将库正确添加到项目中,并且不要忘记导入“UIImageView+WebCache.h”"您希望在何处显示图像。导入图像时显示错误。文件存在何处?如果您将库正确链接到项目,则不会显示错误。阅读指南您可以告诉我如何显示活动指示器,直到图像未显示在表视图单元格中吗?我没有对表视图执行此操作,但是请尝试我更新的答案。我想在我的图库页面中实现这一点,该页面也会像上面的方法一样异步下载图像。很抱歉,这是我的错误。我已经更新了答案。请快速告诉我它是否有效?我想在这里显示指示器。dispatch\u queue\t queue=dispatch\u get\u global\u queue(DISPATCH_QUEUE_PRIORITY_HIGH,0ul);DISPATCH_async(QUEUE,^{NSURL*imageURL=url;NSData*image=[NSData datawithcontentsofull:imageURL];DISPATCH_sync(DISPATCH_get_main_QUEUE(),^{imageview.image=[UIImage imageWithData:data];[scroll addSubview:imageview];[indicator stop animating];); });