首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS -从URL中加载UIImages的更快方法

iOS -从URL中加载UIImages的更快方法
EN

Stack Overflow用户
提问于 2015-10-18 01:06:43
回答 2查看 645关注 0票数 1

我有一个应用程序,当您第一次打开它时,主视图将获取所有的朋友和他们的配置文件图片,并在每个单元格中加载一个UITableView,其中包含朋友的名称、用户名和配置文件图片。问题是如果你有很多朋友的话,UITableView需要花更长的时间来加载所有的朋友,比如大约30分钟(大约需要5-8秒)。我将所有这些图像存储在与朋友用户名相关的字典中,以便以后再使用,但主要问题是,当应用程序第一次启动时,加载所有图像需要多长时间。这是我现在拥有的代码,它可以在遍历每个朋友时执行:

代码语言:javascript
复制
[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和下面的代码:

代码语言:javascript
复制
[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");
    });
}

基本上,我所做的是为每个需要加载的图像将AFHTTPRequestOperation存储在一个NSMutableArray中。在此之后,我遍历了NSMutableArray并启动了每个AFHTTPRequestOperation

问题:

这使得加载所有图像的速度非常快,但我认为我做得不对。在下载完所有图像并将其添加到我的NSMutableDictionary之前,我的活动指示符加载视图就会结束并消失,而在下载和添加所有图像之后,这些图像应该会发生。在所有UIImage下载后台线程完成后,是否有一种方法可以使加载视图和活动指示符消失?

关于AFNetworking的补充问题

我这样做(在这种情况下,制作大约30个图片下载后台线程)是不是在AFNetworking中没有良好的实践?如果一个用户有100多个朋友,同时有100多个图片下载后台线程,这会导致未来的问题吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-18 01:10:34

您可能不会减少下载时间,但您可以使它看起来更快。

您在主线程上使用同步下载,这是一个非常糟糕的主意。

像AFNetworking这样的库使得设置异步下载非常容易。或者,如果您想自己动手,可以使用NSURLSession或NSURLSession (这在iOS 9中是不推荐的)。

票数 3
EN

Stack Overflow用户

发布于 2015-10-18 04:02:46

在使用UIImages时,我经常使用SDWebImage:https://github.com/rs/SDWebImage

您可以查看它的README.md以获得更具体的详细信息,但它通过缓存增加了对来自web的远程图像的支持。

其使用的一个例子:

代码语言:javascript
复制
#import <SDWebImage/UIImageView+WebCache.h>

[self.imageView sd_setImageWithURL:[NSURL URLWithString:imageURL]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33193292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档