这个调用是同步的,所以当它完成时你的图像就准备好了。
您可能想要研究的是异步加载图像并在加载时更新图像。
类似:
NSURL *url = [NSURL URLWithString:imageURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
设置一个属性以包含NSData 并跟踪 NSURLConnection 以便您可以正确释放它。
self.activeDownload = [NSMutableData data];
self.imageConnection = conn;
然后提供适当的委托方法来处理连接回调:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.activeDownload appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Clear the activeDownload property to allow later attempts
self.activeDownload = nil;
// Release the connection now that it's finished
self.imageConnection = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Set appIcon and clear temporary data/image
UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
//MAKE Your callbacks and update the image here.
}
您的回调将要获取该图像,并将其设置为重新加载表格视图将能够正确检索它。 IE。将图像与表格视图单元格使用的其余数据一起设置在数据对象中。
此外,您可以通过以下方式仅对需要它的单元格进行一次刷新:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
这是来自 Apple 自己的 LazyImageTables 示例。