【iOS】SDWebImage的使用

目录

前言

使用方法

与网络申请结合

解决异步


前言

在写知乎日报时,通过接口申请到的网络数据中,有一部分用来表示图片的数据,使用URL表示的,要再次通过URL获取到图片,才能直接使用图片(UIImage),通过SDWebImage第三方库可以很好地解决这个问题,接下来就介绍一下SDWebImage的使用方法。

使用方法

在SDWebImage的官方文档中介绍了很多种使用方法,这里我们介绍其中一种笔者本人在写项目时用到的方法------使用SDWebImageManager

SDWebImage是类别SDWebImageManager背后的类UIImageView(WebCache)。它将异步下载器与图像缓存存储联系起来。您可以直接使用此类,在除 之外的其他环境中UIView(即:使用 Cocoa)利用缓存从 Web 图像下载中获益。

注意:当图片来自内存缓存时,默认情况下不会包含任何图片NSData。但是,如果您需要图片数据,可以传入SDWebImageQueryDataWhenInMemory选项参数。

下面是一个简单的示例:

objectivec 复制代码
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager loadImageWithURL:imageURL
                  options:0
                 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                        // progression tracking code
                 }
                 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                    if (image) {
                        // do something with image
                    }
                 }];

与网络申请结合

当我们要使用网络申请到的图片时,常常需要先申请到该图片的URL,再用这个URL作为上文方法的参数,再在块中调用获取到的image。

解决异步

这样做会出现的问题是,网络申请数据时是异步进行的,而加载图片也是异步进行的,并且加载图片必须在申请到数据之后,所以我们必须确保执行顺序的正确,我的解决办法是使用两次GCD

objectivec 复制代码
dispatch_group_t group = dispatch_group_create();
    dispatch_group_t imageLoadGroup = dispatch_group_create();
    Manager* manager = [Manager shareManeger];
    dispatch_group_enter(group);
    [manager NetWorkGetWithCompletion:^(NSDictionary * _Nonnull userData, NSError * _Nonnull error) {
        self.homeModel = [HomeModel yy_modelWithDictionary:userData];
        self.topMutableArray = (NSMutableArray*)self.homeModel.top_stories;
        for (int i = 0; i < self.homeModel.top_stories.count; i++) {
            TopSubModel* topSubModel = self.topMutableArray[i];
            NSString* iconURL = topSubModel.image;
            NSURL* URL = [NSURL URLWithString:iconURL];
            SDWebImageManager *manager = [SDWebImageManager sharedManager];
            dispatch_group_enter(imageLoadGroup);
            [manager loadImageWithURL:URL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
                if (image) {
                    [self.topIconMutableArray addObject:image];
                    NSLog(@"成功");
//                    NSLog(@"xian:%d",self.topIconMutableArray.count);
                } else {
                    NSLog(@"失败");
                }
                dispatch_group_leave(imageLoadGroup);
            }];
        }
        dispatch_group_leave(group);
    }];
    dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_group_notify(imageLoadGroup, dispatch_get_main_queue(), ^{
            // 在这里进行视图更新
            HomeView* homeView = [[HomeView alloc] init];
            homeView.topIconNSMutableArray = self.topIconMutableArray;
//            NSLog(@"xianzaitop:%d", homeView.topIconNSMutableArray.count);
            homeView.SubModelMutableArray = self.topMutableArray;
//            NSLog(@"xianzai:%d", homeView.SubModelMutableArray.count);
            [homeView.tableView reloadData];
            [self.view addSubview:homeView];
        });
    });

这样就能确保先申请到网络数据,再加载出图片,再更新UI,就可以成功加载出图片了。

这里笔者还只是知道了在这里GCD的使用方法,关于GCD的详细知识,笔者之后会再进行学习,补充在之后的博客中。

相关推荐
songgeb17 小时前
YYImage Bug:一行之差,正常 JPEG 如何在 iOS 被读成条纹
ios·swift
茶底世界之下1 天前
阴天花摊不必变暖:用 Awaking 留住花色与湿街
ios·app·产品
Fatbobman(东坡肘子)2 天前
快速消失的硬盘容量 -- 肘子的 Swift 周报 #160
开发语言·人工智能·redis·ios·swiftui·iphone·swift
茶底世界之下2 天前
斜光下的旧自行车:用 Awaking 留一版复古胶片色
ios·app·产品
2501_916008892 天前
iPhone 设备信息查看,用 KeyMob 看型号、序列号、电池健康和 UDID
android·ios·小程序·https·uni-app·iphone·webview
茶底世界之下3 天前
iPhone Duo 适配实战,Awaking 踩过的 8 个坑
ios·swiftui·swift
跨境数据猎手3 天前
HTTP 响应体编码与压缩:识别、解码
网络协议·http·ios
也非非也3 天前
iPhone 中国区 AI 落地:Apple 与阿里千问合作的技术观察
大数据·人工智能·ios·agent·iphone·智子
茶底世界之下3 天前
同一片泳池,清透还是奶油胶片?用 Awaking 做两版成片
ios
2501_916008893 天前
iOS App 图标、启动屏和多语言搭配 方法,上架前使用KXBuild一次做好 App 图标
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程