看了很多文章,异步加载总是搞不定。下面的代码会在收到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;
}

解决方案 »

  1.   

    [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
      

  2.   

     // Update the table with data
        [theTableView reloadData];这个是不是要重绘的太多了,导致卡的?用局部刷新,方法忘记了,在UITableView里,和reloadData类似的局部刷新。
      

  3.   

    didReceiveData
    会被调用很多次吧,
    这里你需要判断导致接收了是哪个图片,是否接收完成了,然后再决定是否刷新,刷新哪个cell.