【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的详细知识,笔者之后会再进行学习,补充在之后的博客中。

相关推荐
胖虎13 分钟前
SwiftUI 页面作为一级页面数据被重置问题分析
ios·swiftui·swift·state·observedobject·stateobject·swiftui页面生命周期
健了个平_244 小时前
【iOS】如何在 iOS 26 的UITabBarController中使用自定义TabBar
ios·swift·wwdc
Digitally7 小时前
无需 iTunes 将文件从 PC 传输到 iPhone
ios·iphone
1024小神9 小时前
xcode 配置了AppIcon 但是不显示icon图标
ios·swiftui·swift
2501_9159184111 小时前
iOS 项目中证书管理常见的协作问题
android·ios·小程序·https·uni-app·iphone·webview
ITKEY_11 小时前
iOS网页应用无地址栏无工具栏
ios
2501_9159184111 小时前
提升 iOS 应用安全审核通过率的一种思路,把容易被拒的点先处理
android·安全·ios·小程序·uni-app·iphone·webview
RollingPin12 小时前
iOS探究使用Block方式实现一对多回调能力
ios·block·runtime·数据分发·解耦·动态绑定·一对多回调
TheNextByte112 小时前
iPhone短信备份与恢复:3种最佳方法及短信备份与恢复应用
ios·iphone
2501_9160088912 小时前
iOS 应用发布流程中常被忽视的关键环节
android·ios·小程序·https·uni-app·iphone·webview