iOS - 从 URL 加载 UIImages 的更快方法
iOS - Faster Way To Load UIImages From URL's
我有一个应用程序,当你第一次打开它时,主视图会获取你所有的朋友和他们的个人资料照片,并在每个单元格中加载一个 UITableView
,其中包含你朋友的姓名、用户名和个人资料照片。问题是如果你有很多朋友,UITableView
需要更长的时间来加载所有朋友,例如大约 30 个(大约需要 5-8 秒)。我将所有这些图像存储在与朋友的用户名相关联的字典中,以便以后可以重复使用它们,但主要问题是应用程序首次启动时加载所有图像需要多长时间。这是我现在拥有的代码,它在遍历每个朋友时执行此操作:
[auth.loggedInUser getFriendsSuccessCallback:^(NSArray* friends){
for (User* friend in friends) {
if (![weakSelf.friendImageDictionary.allKeys containsObject:friend.userName])
{
UIImage *profilePicture;
if (!friend.profilePicture) {
profilePicture = [UIImage imageNamed:@"ProfilePic"];
}
else
{
NSURL *profilePictureURL = [NSURL URLWithString:friend.profilePicture];
NSData * profilePictureData = [NSData dataWithContentsOfURL:profilePictureURL];
profilePicture = [UIImage imageWithData:profilePictureData];
}
if (profilePicture == nil)
{
profilePicture = [UIImage imageNamed:@"ProfilePic"];
}
[weakSelf.friendImageDictionary setObject:profilePicture forKey:friend.userName];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.tableView reloadData];
});
}
关于如何显着减少加载所有这些图像所需的时间,有什么建议吗?
进度:
好的,我正在使用 AFNetworking
和以下代码:
[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");
});
}
基本上我所做的是为每个需要加载的图像在 NSMutableArray
中存储一个 AFHTTPRequestOperation
。之后,我遍历 NSMutableArray
并开始每个 AFHTTPRequestOperation
。
问题:
这使得加载所有图像的速度非常快,但我认为我做错了。我的 activity 指示器的加载视图在所有图像完成下载并添加到我的 NSMutableDictionary
之前完成并消失,当它应该发生在所有图像被下载和添加之后。有没有办法让我的加载视图和 activity 指示器在所有 UIImage
下载后台线程完成后消失?
关于 AFNetworking
的其他问题:
我这样做的方式(在这种情况下使大约 30 个图像下载后台线程同时发生)不是 AFNetworking
的好习惯吗?如果一个用户有超过100个朋友并且有超过100个图片下载后台线程同时发生,是否会导致将来出现问题?
你可能不会减少下载时间,但你可以让它看起来更快。
您正在主线程上使用同步下载,这是一个非常糟糕的主意。
像 AFNetworking 这样的库可以让设置异步下载变得非常容易。或者,如果你想自己做,你可以使用 NSURLSession
,或 NSURLSession(在 iOS 9 中被弃用。)
在使用 UIImages 时,我经常使用 SDWebImage:https://github.com/rs/SDWebImage
您可以查看它的 README.md 了解更具体的细节,但它增加了对来自 Web 的远程图像的支持,并带有缓存。
使用示例:
#import <SDWebImage/UIImageView+WebCache.h>
[self.imageView sd_setImageWithURL:[NSURL URLWithString:imageURL]
我有一个应用程序,当你第一次打开它时,主视图会获取你所有的朋友和他们的个人资料照片,并在每个单元格中加载一个 UITableView
,其中包含你朋友的姓名、用户名和个人资料照片。问题是如果你有很多朋友,UITableView
需要更长的时间来加载所有朋友,例如大约 30 个(大约需要 5-8 秒)。我将所有这些图像存储在与朋友的用户名相关联的字典中,以便以后可以重复使用它们,但主要问题是应用程序首次启动时加载所有图像需要多长时间。这是我现在拥有的代码,它在遍历每个朋友时执行此操作:
[auth.loggedInUser getFriendsSuccessCallback:^(NSArray* friends){
for (User* friend in friends) {
if (![weakSelf.friendImageDictionary.allKeys containsObject:friend.userName])
{
UIImage *profilePicture;
if (!friend.profilePicture) {
profilePicture = [UIImage imageNamed:@"ProfilePic"];
}
else
{
NSURL *profilePictureURL = [NSURL URLWithString:friend.profilePicture];
NSData * profilePictureData = [NSData dataWithContentsOfURL:profilePictureURL];
profilePicture = [UIImage imageWithData:profilePictureData];
}
if (profilePicture == nil)
{
profilePicture = [UIImage imageNamed:@"ProfilePic"];
}
[weakSelf.friendImageDictionary setObject:profilePicture forKey:friend.userName];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.tableView reloadData];
});
}
关于如何显着减少加载所有这些图像所需的时间,有什么建议吗?
进度:
好的,我正在使用 AFNetworking
和以下代码:
[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");
});
}
基本上我所做的是为每个需要加载的图像在 NSMutableArray
中存储一个 AFHTTPRequestOperation
。之后,我遍历 NSMutableArray
并开始每个 AFHTTPRequestOperation
。
问题:
这使得加载所有图像的速度非常快,但我认为我做错了。我的 activity 指示器的加载视图在所有图像完成下载并添加到我的 NSMutableDictionary
之前完成并消失,当它应该发生在所有图像被下载和添加之后。有没有办法让我的加载视图和 activity 指示器在所有 UIImage
下载后台线程完成后消失?
关于 AFNetworking
的其他问题:
我这样做的方式(在这种情况下使大约 30 个图像下载后台线程同时发生)不是 AFNetworking
的好习惯吗?如果一个用户有超过100个朋友并且有超过100个图片下载后台线程同时发生,是否会导致将来出现问题?
你可能不会减少下载时间,但你可以让它看起来更快。
您正在主线程上使用同步下载,这是一个非常糟糕的主意。
像 AFNetworking 这样的库可以让设置异步下载变得非常容易。或者,如果你想自己做,你可以使用 NSURLSession
,或 NSURLSession(在 iOS 9 中被弃用。)
在使用 UIImages 时,我经常使用 SDWebImage:https://github.com/rs/SDWebImage
您可以查看它的 README.md 了解更具体的细节,但它增加了对来自 Web 的远程图像的支持,并带有缓存。
使用示例:
#import <SDWebImage/UIImageView+WebCache.h>
[self.imageView sd_setImageWithURL:[NSURL URLWithString:imageURL]