【发布时间】:2015-10-20 00:37:15
【问题描述】:
我正在尝试使用 AFNetworking 从 URL 异步下载图像,而不是在我的应用程序中一一下载,当您打开它时,主视图会获取您所有的朋友及其个人资料图片并加载 @987654322 @ 在每个单元格中包含您朋友的姓名、用户名和个人资料图片。这是我正在使用的代码:
[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 在所有图像下载后台线程完成之前被重新加载,这导致UITableViewCells 中只有一些个人资料图片。我怎样才能等待所有这些都完成,然后我才能重新加载UITableView?
另外,我很怀疑我是否正确地执行此操作。我这样做的方式(在这种情况下同时发生大约 30 个图像下载后台线程)不是 AFNetworking 的好习惯吗?假设一个用户有超过 100 个朋友,并且同时有超过 100 个图片下载后台线程发生,是否会导致未来的问题?
【问题讨论】:
标签: ios objective-c uitableview uiimage afnetworking-2