异步加载tableview图片

roman9t 2011-10-27 08:16:41
看了很多文章,异步加载总是搞不定。下面的代码会在收到json的时候下载全部图片(包括大图和小图),ui线程会很卡,请教改如何修改?感谢。


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Store incoming data into a string
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

// Create a dictionary from the JSON string
NSDictionary *results = [jsonString JSONValue];

// Build an array from the dictionary for easy access to each entry
NSArray *photos = [[results objectForKey:@"photos"] objectForKey:@"photo"];

// Loop through each entry in the dictionary...
for (NSDictionary *photo in photos)
{
// Get title of the image
NSString *title = [photo objectForKey:@"title"];

// Save the title to the photo titles array
[photoTitles addObject:(title.length > 0 ? title : @"Untitled")];

NSString *photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_s.jpg", [photo objectForKey:@"farm"], [photo objectForKey:@"server"], [photo objectForKey:@"id"], [photo objectForKey:@"secret"]];
[photoSmallImageData addObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURLString]]];

photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_m.jpg", [photo objectForKey:@"farm"], [photo objectForKey:@"server"], [photo objectForKey:@"id"], [photo objectForKey:@"secret"]];
[photoURLsLargeImage addObject:[NSURL URLWithString:photoURLString]];
}

// Update the table with data
[theTableView reloadData];

// Stop the activity indicator
[activityIndicator stopAnimating];

[jsonString release];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
BigPicViewController *bpvc = [[BigPicViewController alloc] init];
bpvc.array = [photoURLsLargeImage objectAtIndex:indexPath.row];
[self.navigationController pushViewController:bpvc animated:YES];
[bpvc release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cachedCell"];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cachedCell"] autorelease];

#if __IPHONE_3_0
cell.textLabel.text = [photoTitles objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:13.0];
#else
cell.text = [photoTitles objectAtIndex:indexPath.row];
cell.font = [UIFont systemFontOfSize:13.0];
#endif

NSData *imageData = [photoSmallImageData objectAtIndex:indexPath.row];

#if __IPHONE_3_0
cell.imageView.image = [UIImage imageWithData:imageData];
#else
cell.image = [UIImage imageWithData:imageData];
#endif

return cell;
}
...全文
613 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序员小迷 2012-01-12
  • 打赏
  • 举报
回复
didReceiveData
会被调用很多次吧,
这里你需要判断导致接收了是哪个图片,是否接收完成了,然后再决定是否刷新,刷新哪个cell.
程序员小迷 2012-01-12
  • 打赏
  • 举报
回复
// Update the table with data
[theTableView reloadData];

这个是不是要重绘的太多了,导致卡的?

用局部刷新,方法忘记了,在UITableView里,和reloadData类似的局部刷新。
Matt 2011-11-06
  • 打赏
  • 举报
回复
[photoSmallImageData addObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURLString]]];//这个其中的dataWithContentsOfURL才是你需要做异步处理的地方吧,下载需要时间的啊

用NSURLConnection解决
[[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL: url] delegate:self];

另外记得响应NSURLConnection的delegate.
1.didReceiveData
2.didFailWithError
3.connectionDidFinishLoading
roman9t 2011-10-27
  • 打赏
  • 举报
回复
就你一个人。。。看来分要是你的了。。

[Quote=引用 1 楼 yxy353245805 的回复:]

顶一下。。。。。。。
[/Quote]
yxy353245805 2011-10-27
  • 打赏
  • 举报
回复
顶一下。。。。。。。

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧