我正在使用NSData dataWithContentsOfURL方法在tableview中显示图像,但是当我滚动tableview GUI时,hanged.so在搜索论坛后发现我可以尝试使用NSUrlConnection方法。所以我试过但我无法成功实现它。
请在下面找到我的代码......
请帮助我,我怎么能正确地做到这一点..
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"DataIdentifier"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:249.0/255.0 blue:230.0/255.0 alpha:2.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
profileName = [appDelegate.arrCommunityUserList objectAtIndex:indexPath.row];
NSString *imgName = [profileName.user_image stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *strValue = [NSString stringWithFormat:@"%d", profileName.userID];
if (tableView == myTableView)
{
cellRectangle = CGRectMake(15, 2, 75, 75 );
NSString *myurl = [NSString stringWithFormat: @"%@pics/photos/%@/%@",ConstantImgURL, strValue,imgName];
NSURL *url = [NSURL URLWithString: myurl];
imageView = [[UIImageView alloc] initWithFrame: cellRectangle];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:myurl]];
[NSURLConnection connectionWithRequest:request delegate:self];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest:
request delegate:self];
if (theConnection)
{
receivedData = [[NSMutableData data] retain];
}
[cell.contentView addSubview:imageView];
}
}
return cell;
}
// did receive response
- ( void )connection:( NSURLConnection * )connection didReceiveResponse:( NSURLResponse * )response
//--------------------------------------------------------------------------------------------------
{
NSLog(@"Received response: %@", response);
}
// get recieved data
- ( void )connection:( NSURLConnection * )connection didReceiveData:( NSData * )data
//----------------------------------------------------------------------------------
{
// NSLog(@"Connection received data, retain count: %d", [connection retainCount]);
[receivedData appendData:data];
}
// finished loading
- ( void )connectionDidFinishLoading:( NSURLConnection * )connection
//-------------------------------------------------------------------
{
// Set appIcon and clear temporary data/image
UIImage *image = [[UIImage alloc] initWithData:receivedData];
imageView.image = image;
}
// connection failed with error
- ( void )connection:( NSURLConnection * )connection didFailWithError:( NSError * )connError
//---------------------------------------------------------------------------------------
{
// NSLog(@"Error receiving response: %@", connError);
[connection release];
[receivedData release];
}
答案 0 :(得分:3)
dataWithContentsOfURL
是同步网络请求。这意味着在调用代码的地方,它将等待请求完成,然后再继续执行下一条指令。同步网络不好。特别糟糕。它只能用于测试。
您应该做的是为这些图像触发异步请求。上面的代码非常慢的原因是tableView
每次要求cellForRowAtIndexPath:
的dataSource委托;您的代码会同步触发网络请求 - 这意味着在图像的网络请求完成之前,不会返回单元格。
相反,您应该做的是在请求tableView
时异步加载所有图像。 Here's a good example使用标记在返回时标识它们。在你正在做的事情的整个背景下,这并不容易;所以也许您可能希望在显示tableView
时启动所有NSURLConnections,为0
返回numberOfSectionsInTableView
直到连接完成,然后在reloadData
上调用tableView
当它们全部完成时(并使numberOfSectionsInTableView
现在返回要显示的正确行数)。
答案 1 :(得分:0)
开始使用ASI库:http://allseeing-i.com/ASIHTTPRequest/How-to-use 越快越好。
答案 2 :(得分:0)
我认为这可能会解决您的问题... http://www.markj.net/iphone-asynchronous-table-image/