我正在尝试使用AFNetworking
从网址异步下载图片而不是在我的应用中逐个下载图片,当您打开它时,主视图会获取所有朋友及其个人资料图片并加载UITableView
在每个单元格中显示您朋友的姓名,用户名和个人资料照片。这是我正在使用的代码:
[auth.loggedInUser getFriendsSuccessCallback:^(NSArray* friends){
NSMutableArray *profilePictureRequests = [[NSMutableArray alloc] init];
for (User* friend in friends) {
if (![weakSelf.friendImageDictionary.allKeys containsObject:friend.userName])
{
if (!friend.profilePicture) {
UIImage *profilePicture = [UIImage imageNamed:@"ProfilePic"];
[weakSelf.friendImageDictionary setObject:profilePicture forKey:friend.userName];
}
else
{
NSURL *profilePictureURL = [NSURL URLWithString:friend.profilePicture];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:profilePictureURL];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, UIImage* profilePicture) {
NSLog(@"Response: %@", profilePicture);
if (profilePicture == nil)
{
profilePicture = [UIImage imageNamed:@"ProfilePic"];
}
[weakSelf.friendImageDictionary setObject:profilePicture forKey:friend.userName];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image error: %@", error);
}];
[profilePictureRequests addObject:requestOperation];
}
}
}
for (AFHTTPRequestOperation *requestOperation in profilePictureRequests)
{
[requestOperation start];
}
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.tableView reloadData];
weakSelf.dashboardViewController.loadingCoverView.hidden = YES;
[weakSelf.dashboardViewController.tableActivityIndicator stopAnimating];
NSLog(@"FINISHED LOADING ALL PROFILE PICTURES");
});
}
问题在于,在所有图像下载后台线程完成之前,我的UITableView
正在重新加载,导致UITableViewCell
s只有一些配置文件图片。我怎样才能等待所有人完成所以我可以重新加载UITableView
?
另外,我是偏执狂我是否正确地这样做了。我正在这样做的方式(在这种情况下同时发生大约30个图像下载后台线程)AFNetworking不是很好的做法吗?如果,假设一个用户有超过100个朋友并且同时发生超过100个图像下载后台线程会导致未来出现问题吗?
答案 0 :(得分:1)
重新加载整个TableView可能会导致一些问题。您应该只在屏幕上更新单元格的ImageView。这称为延迟加载,并且您无需下载可能永远不会看到的图像。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
NSInteger currentTag = cell.tag + 1;
cell.tag = currentTag;
User *user = self.friends[indexPath.row];
NSURL *profilePictureURL = [NSURL URLWithString:user.profilePicture];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:profilePictureURL];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
op.responseSerializer = [AFImageResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, UIImage* image) {
//Check that the cell hasn't been reused
dispatch_async(dispatch_get_main_queue(), ^{
if (cell.tag == currentTag) {
[cell.imageView setImage: image];
}
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//Error
}];
return cell;
}
我无法回想起我的问题,但AFNetworking可能会为您缓存响应,如果没有,您可以随时自行保存以免再次下载图像。
答案 1 :(得分:0)
如果有疑问,请使用类似这样的框架:
https://github.com/rs/SDWebImage https://github.com/path/FastImageCache
他们会把你正在做的所有这些事情拿走,并且会节省你的时间和金钱,让你的应用程序尽快到达Appstore!