看了很多文章,异步加载总是搞不定。下面的代码会在收到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;
}
- (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;
}
解决方案 »
- IOS项目如何导入包
- 数字证书
- touchesMoviedwithevent 或者点图片移动具体的实现操作
- 动态生成二维数组
- 我是嵌入式C软件开发者,工作2年,要是从零开始开发IOS的APP,要多久能成型?
- ios语音控制
- 想做一个加速平移的动画,非游戏,想知道加速怎么实现?
- 望有高人指点一下如何学习ios开发这方面的东西~~
- 请问在app store 上发布项目,怎么控制兼容性呢?我发布的项目都是iOS 8.0以上,想控制在7.0以上怎么做?
- 高手看过来!求解,坐等回复啊!看了好几天,没有头绪。
- 关于windows和mac下共用代码(OC中嵌入C++的问题)
- 这段代码如何转成android代码?
用NSURLConnection解决
[[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL: url] delegate:self];
另外记得响应NSURLConnection的delegate.
1.didReceiveData
2.didFailWithError
3.connectionDidFinishLoading
[theTableView reloadData];这个是不是要重绘的太多了,导致卡的?用局部刷新,方法忘记了,在UITableView里,和reloadData类似的局部刷新。
会被调用很多次吧,
这里你需要判断导致接收了是哪个图片,是否接收完成了,然后再决定是否刷新,刷新哪个cell.