美文网首页iOS 开发
IOS 对三方库的整理

IOS 对三方库的整理

作者: 已注销哈哈 | 来源:发表于2016-07-30 14:50 被阅读108次

    NFNetWorking的整理:                                                                                           AFNetworking是一个非常方便的网络请求库,可以轻松实现各种网络请求,比如经常使用的GET请求、POST请求,以及上传多张图片等,下面介绍一下基本的使用方法。


    首先下载AFNetworking 然后在需要使用的类中,导入如下2个头文件:

    AFNetworking 3.0正式支持的iOS 7, Mac OS X的10.9, watchOS 2 , tvOS 9 和Xcode 7。如果你想使用AFNetworking在针对较旧版本的SDK项目,请检查README的兼容性信息。

    ASIHttpRequest,有三年多没更新,而且还是MRC,跟不上技术的发展了,在ARC基础上全部都是使用AFNetWorking:ARC下的一个网络请求库,还在不断的更新,使用简单方便


    发请求:全部都是异步请求,没有同步请求的方法。使用manager可以发起GET/PUT/PATCH...请求.

    (1.)创建网络请求管理类                                                                                                        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];                          AFNetworking:默认只支持三种响应头:application/json  text/json  text/javascrip            我们常用的还有一种:text/html 如果服务器需要该类型,则设置下面的参数                            (请求失败:在error中出现类似 text/html 这种类型的参数是再设置).                                      (2.)请求体                                                                                                              manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];                                                                                        (3.)get请求                                                                                                                         [manager GET:@"http://localhost:8080/Login/NewServlet?command=5"         parameters:nil       progress:^(NSProgress * _Nonnull downloadProgress) {                                                                                                                                                                             } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {                                          //处理请求成功的事件                                                                                                                          //AFNetworking:返回参数是json,AFNetworking会自动返回json解析后的数据(NSArray 、 NSDictionary)           //不需要我们使用NSJSONSerialization解析json数据                                                                                                if ([responseObject isKindOfClass:[NSData class]]) {                                                       NSLog(@"data--------->%@", [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]); }else if ([responseObject isKindOfClass:[NSDictionary class]]) {

    NSLog(@"responseObject -----> %@", responseObject);                                                                                    }       

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {                                                  //处理请求失败的事件                                                                                                                           NSLog(@"error -----> %@", error);                                                                                                                      }];


    请求数据格式                                                                                                                     AFHTTPRequestSerializer     二进制格式                                                                     AFJSONRequestSerializer     JSON                                                                AFPropertyListRequestSerializer    PList(是一种特殊的XML,解析起来相对容易)                                                       响应数据格式                                                                                                                    AFHTTPResponseSerializer   二进制格式                                                                     AFJSONResponseSerializer   JSON                                                                 AFXMLParserResponseSerializer     XML,只能返回XMLParser,还需要自己通过代理方法解析    AFXMLDocumentResponseSerializer (Mac OS X)                                             AFPropertyListResponseSerializer   PList                                                       AFImageResponseSerializer          Image                                                         AFCompoundResponseSerializer      组合


                                                                                                                                                                            请求数据格式                                                                                                                AFHTTPRequestSerializer     二进制格式                                                                    AFJSONRequestSerializer     JSON                                                                      AFPropertyListRequestSerializer    PList(是一种特殊的XML,解析起来相对容易)


    响应数据格式                                                                                                                   AFHTTPResponseSerializer   二进制格式                                                                   AFJSONResponseSerializer   JSON                                                                   AFXMLParserResponseSerializer     XML,只能返回XMLParser,还需要自己通过代理方法解析           AFXMLDocumentResponseSerializer (Mac OS X)                                           AFPropertyListResponseSerializer   PList                                                                 AFImageResponseSerializer        Image                                                                                       AFCompoundResponseSerializer      组合请求数据格式                                                  AFHTTPRequestSerializer     二进制格式                                                                     AFJSONRequestSerializer     JSON                                                               AFPropertyListRequestSerializer    PList(是一种特殊的XML,解析起来相对容易)

    响应数据格式                                                                                                                  AFHTTPResponseSerializer   二进制格式                                                                   AFJSONResponseSerializer   JSON                                                                    AFXMLParserResponseSerializer     XML,只能返回XMLParser,还需要自己通过代理方法解析   AFXMLDocumentResponseSerializer (Mac OS X)                                            AFPropertyListResponseSerializer   PList                                                      AFImageResponseSerializer          Image                                                        AFCompoundResponseSerializer       组合


    (4.)POST请求                                                                                 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];                   [manager POST:@"http://localhost:8080/Login/NewServlet" parameters:parma progress:^(NSProgress * _Nonnull uploadProgress) {                                                                                                                                  } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

    NSLog(@"------->%@", responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    NSLog(@"error ------> %@", error);

    }];

    (5.)稍微插入一点小知识                                                                                                                                         //检测网络状态                                                                                                               [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {          if (status == 0) {                                                                                                                                       NSLog(@"没有网络");                                                                                                                                       }else if (status == 1) {                                                                                                                               NSLog(@"移动网络");                                                                                                                                         }else if (status == 2) {                                                                                                                               NSLog(@"WiFi");                                                                                                                                                     }                                                                                                                                                                            }];

    //开始检测                                                                                                                        [manager.reachabilityManager startMonitoring];


    SDWebImage的使用                                                                                                          第一步,下载SDWebImage,导入工程。                                                                         github托管地址https://github.com/rs/SDWebImage                                                          第二步,在需要的地方导入头文件                                                                                        #import "UIImageView+WebCache.h"                                                                                

    导入头文件

    然后再说说加载图片的一些流程                                                                                            1.入口setImageWithURL:placeholderImage:options: 会先把 placeholderImage 显示,然后 SDWebImageManager 根据 URL 开始处理图片。                                                                  2.进入SDWebImageManager-downloadWithURL:delegate:options:userInfo:,交给SDImageCache 从缓存查找图片是否已经下载 queryDiskCacheForKey:delegate:userInfo:.    3.先从内存图片缓存查找是否有图片,如果内存中已经有图片缓存,SDImageCacheDelegate 回调 imageCache:didFindImage:forKey:userInfo: 到 SDWebImageManager。                       4.如果内存缓存中没有,生成 NSInvocationOperation 添加到队列开始从硬盘查找图片是否已经缓存。                                                                                                                              5.根据 URLKey 在硬盘缓存目录下尝试读取图片文件。这一步是在 NSOperation 进行的操作,所以回主线程进行结果回调notifyDelegate:。                                                                          6.如果上一操作从硬盘读取到了图片,将图片添加到内存缓存中(如果空闲内存过小,会先清空内存缓存)。SDImageCacheDelegate 回调imageCache:didFindImage:forKey:userInfo:。进而回调展示图片。                                                                                                                 7.如果从硬盘缓存目录读取不到图片,说明所有缓存都不存在该图片,需要下载图片,回调imageCache:didNotFindImageForKey:userInfo:。                                                             8.共享或重新生成一个下载器SDWebImageDownloader 开始下载图片。                                   9.图片下载由 NSURLConnection 来做,实现相关 delegate 来判断图片下载中、下载完成和下载失败。                                                                                      10.connection:didReceiveData:中利用 ImageIO 做了按图片下载进度加载效果。11.connectionDidFinishLoading:数据下载完成后交给 SDWebImageDecoder做图片解码处理。                                                                                                                               12.图片解码处理在一个 NSOperationQueue 完成,不会拖慢主线程 UI。如果有需要对下载的图片进行二次处理,最好也在这里完成,效率会好很多。                                                           13.在主线程 notifyDelegateOnMainThreadWithInfo:宣告解码完成,imageDecoder:didFinishDecodingImage:userInfo:回调给SDWebImageDownloader.     14.imageDownloader:didFinishWithImage:回调给 SDWebImageManager 告知图片下载完成。                                                                                                                               15.通知所有的 downloadDelegates 下载完成,回调给需要的地方展示图片。                       16.将图片保存到 SDImageCache 中,内存缓存和硬盘缓存同时保存。写文件到硬盘也在以单独 NSInvocationOperation 完成,避免拖慢主线程。                                                 17.SDImageCache 在初始化的时候会注册一些消息通知,在内存警告或退到后台的时候清理内存图片缓存,应用结束的时候清理过期图片。                                                                      18.SDWI 也提供了 UIButton+WebCache和 MKAnnotationView+WebCache,方便使用。19.SDWebImagePrefetcher可以预先下载图片,方便后续使用                                                

    从上面流程可以看出,当你调用setImageWithURL:方法的时候,他会自动去给你干这么多事,当你需要在某一具体时刻做事情的时候,你可以覆盖这些方法。比如在下载某个图片的过程中要响应一个事件,就覆盖这个方法:

    SDWebImage库的作用还是要拿出来讲一下                                                                           通过对UIImageView的类别扩展来实现异步加载替换图片的工作。                                                                                                                                                                                   主要用到的对象:                                                                                                              1、UIImageView (WebCache)类别,入口封装,实现读取图片完成后的回调                            2、SDWebImageManager,对图片进行管理的中转站,记录那些图片正在读取。向下层读取Cache(调用SDImageCache),或者向网络读取对象(调用SDWebImageDownloader) 。实现SDImageCache和SDWebImageDownloader的回调。                                                    3、SDImageCache,根据URL的MD5摘要对图片进行存储和读取(实现存在内存中或者存在硬盘上两种实现)实现图片和内存清理工作。                                                                              4、SDWebImageDownloader,根据URL向网络读取数据(实现部分读取和全部读取后再通知回调两种方式)                                                                                                                      SDWebImage的用法还有很多,需要在日后的编程中去发现


    相关文章

      网友评论

        本文标题:IOS 对三方库的整理

        本文链接:https://www.haomeiwen.com/subject/cnizjttx.html