我正异步下载表中的图像。当表第一次出现时,然后连接:didrecieveData没有被调用,但当我向上或向下移动时,它被调用并显示图像。请帮助
表查看类:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
customiseForVideos *cell =(customiseForVideos *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"customiseForVideos" owner:self options:nil];
cell=tblCell;
}
else{
AsyncImageView* oldImage = (AsyncImageView*)
[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}
[cell.imageActivity startAnimating];
CGRect frame;
frame.size.width=65; frame.size.height=70;
frame.origin.x=24; frame.origin.y=12;
AsyncImageView* asyncImage = [[[AsyncImageView alloc]
initWithFrame:frame] autorelease];
asyncImage.tag = 999;
NSURL* url = [NSURL URLWithString:[[videoCollection objectAtIndex:indexPath.row] videoImageUrl]];
//NSLog(@"%@",url);
[asyncImage loadImageFromURL:url activity:cell.imageActivity];
[cell.contentView addSubview:asyncImage];
if((indexPath.row%2)==0)
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"green-cell-row.png"]] autorelease];
else{
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue-cell-row.png"]] autorelease];
}
return cell;
}
AsyncImageView类:
- (void)loadImageFromURL:(NSURL*)url activity:(UIActivityIndicatorView *)cellActivity {
activityOnCell=cellActivity;
activityOnCell.hidden=NO;
if (self.connection!=nil) { [self.connection release]; } //in case we are downloading a 2nd image
if (data!=nil) { [data release]; }
NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //notice how delegate set to self object
NSLog(@"%@",request);
//TODO error handling, what if connection is nil?
}
//the URL connection calls this repeatedly as data arrives
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; }
[data appendData:incrementalData];
}
//the URL connection calls this once all the data has downloaded
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
//so self data now has the complete image
[self.connection release];
self.connection=nil;
if ([[self subviews] count]>0) {
//then this must be another image, the old one is still in subviews
[[[self subviews] objectAtIndex:0] removeFromSuperview]; //so remove it (releases it also)
}
//make an image view for the image
UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];
//make sizing choices based on your needs, experiment with these. maybe not all the calls below are needed.
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
[self addSubview:imageView];
imageView.frame = self.bounds;
[imageView setNeedsLayout];
[self setNeedsLayout];
activityOnCell.hidden=YES;
[data release]; //don't need this any more, its in the UIImageView now
data=nil;
}
答案 0 :(得分:2)
将图像加载到TableView中是一个常见问题。以至于解决方案有所下降。您最好使用其中一种,然后尝试设计自己的。您将节省大量时间,通常可以找到优化的解决方案,如果您需要更多功能,可以随时进行子类化。
每次进入新图像时重新加载表都不是最佳解决方案。当你在用户滚动时重新加载时,他会失去他的位置。
使用https://github.com/AFNetworking/AFNetworking
这就是做你需要的事情是多么容易。
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];
答案 1 :(得分:1)
收到图像后,您没有重新加载tableview。使用
[mtableView reloadData];
连接完成加载。如果它不是tableview所在的同一个类,那么创建另一个方法,然后放置重载表并通过委托从其他类调用它。
答案 2 :(得分:0)
有一个很棒的第三方软件包可以在一行代码中为您完成所有事情: https://github.com/rs/SDWebImage
您需要做的就是导入此类别(#import "UIImageView+WebCache.h"
)
然后在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}
中,您应该使用一种简单的方法设置单元格的图像:
[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"noPic.png"]];
你完成了:)
答案 3 :(得分:0)
使用NSCache临时存储图像,并使用url作为键,使用NSData作为值。
并在方法cellforRowAtIndexPath中从NSCache对象获取值并将其分配给imageview。它将停止闪烁并且可见。