星空网 > 软件开发 > 操作系统

IOS 网络浅析-(六 网络图片获取之三方SDWebImage

网络图片获取是大多数app所能用到的,由于实际app开发中原生api很少用到,在这里就先不介绍了,以后有时间会给大家介绍。这篇文章会给大家介绍一个三方-SDWebImage-master。SDWebImage-master 是一个非常强大的三方。今天给大家主要讲一些主要功能。其他功能可以下载下来自己琢磨琢磨。

*温馨提示:SDWebImage-master可以在github上下载。*

当需要应用SDWeb时把文件夹里的SDWebImage文件夹放入工程里。

在需要使用网络获取图片的文件里进入头文件#import "UIImageView+WebCache.h"即可使用相应的api。

首先最基本的方法展示(仅获取图片)

代码:

//// ViewController.m// CX-SDWebImage-master//// Created by ma c on 16/3/18.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "UIImageView+WebCache.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];    NSString * urlString = [NSString stringWithFormat:@"http://localhost/tupian.jpg"];    NSURL * url = [NSURL URLWithString:urlString];    UIImageView * imageView = [[UIImageView alloc]init];    imageView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), 300);    [imageView sd_setImageWithURL:url];    [self.view addSubview:imageView];  }@end

 在很多情况下由于网络的关系,图片无法下载,这是会存在留白现象,这将会是一种很不好的app体验,因此,为了解决这一弊端,此三方还有占位图片(在下载图片的时候,此图片心事出来。)

代码:

//// ViewController.m// CX-SDWebImage-master//// Created by ma c on 16/3/18.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "UIImageView+WebCache.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];    NSString * urlString = [NSString stringWithFormat:@"http://localhost/tupian.jpg"];    NSURL * url = [NSURL URLWithString:urlString];    UIImageView * imageView = [[UIImageView alloc]init];    imageView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), 300);    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"图片名称"]];    [self.view addSubview:imageView];  }@end

依旧是有些时候(我也不知道为什么会有这么多有些时候)下载完图片需要某些操作,因此block是一种很不错的选择。

代码:

 

//// ViewController.m// CX-SDWebImage-master//// Created by ma c on 16/3/18.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "UIImageView+WebCache.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];    NSString * urlString = [NSString stringWithFormat:@"http://localhost/tupian.jpg"];    NSURL * url = [NSURL URLWithString:urlString];    UIImageView * imageView = [[UIImageView alloc]init];    imageView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), 300);    [imageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {        NSLog(@"%@",image);    //枚举    NSLog(@"%ld",(long)cacheType);        NSLog(@"%@",imageURL);      }];    [self.view addSubview:imageView];  }/* 结果 2016-03-18 13:12:18.091 CX-SDWebImage-master[4626:283982] <UIImage: 0x7fe8a3f0d900>, {250, 250} 2016-03-18 13:12:18.092 CX-SDWebImage-master[4626:283982] 1 2016-03-18 13:12:18.092 CX-SDWebImage-master[4626:283982] http://localhost/tupian.jpg */@end

 再接着...

很多时候我们查看图片,也就是下载图片的时候,图片上有🈶圈圈不停的转,这个很好理解,就是简单的风火轮。

但是还有一种现象就是下载的进度。下载进度是怎么实现的的。

下面看代码里是 如何实现的吧。

代码:

//// ViewController.m// CX-SDWebImage-master//// Created by ma c on 16/3/18.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "UIImageView+WebCache.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];    NSString * urlString = [NSString stringWithFormat:@"http://localhost/tupian.jpg"];    NSURL * url = [NSURL URLWithString:urlString];    UIImageView * imageView = [[UIImageView alloc]init];    imageView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), 300);    /*   options所有选项:     //失败后重试 (经常使用)   SDWebImageRetryFailed = 1 << 0,            //UI交互期间开始下载,导致延迟下载比如UIScrollView减速。(经常使用)   SDWebImageLowPriority = 1 << 1,            //只进行内存缓存   SDWebImageCacheMemoryOnly = 1 << 2,            //这个标志可以渐进式下载,显示的图像是逐步在下载   SDWebImageProgressiveDownload = 1 << 3,            //刷新缓存   SDWebImageRefreshCached = 1 << 4,            //后台下载   SDWebImageContinueInBackground = 1 << 5,            //优先下载   SDWebImageHighPriority = 1 << 8,            //延迟占位符   SDWebImageDelayPlaceholder = 1 << 9,            //改变动画形象   SDWebImageTransformAnimatedImage = 1 << 10,     */    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"这里是占位图片"] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {//    receivedSize 已经下载大小//    expectedSize 原大小//    动动我们聪明的脑袋,该怎么做呢??//    在这里我就不实现了,大家可以多联系联系。          } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {        NSLog(@"下载完成");      }];    [self.view addSubview:imageView];  }/* 结果 2016-03-18 13:12:18.091 CX-SDWebImage-master[4626:283982] <UIImage: 0x7fe8a3f0d900>, {250, 250} 2016-03-18 13:12:18.092 CX-SDWebImage-master[4626:283982] 1 2016-03-18 13:12:18.092 CX-SDWebImage-master[4626:283982] http://localhost/tupian.jpg */@end

对于三方SDWebImage-master还有很多的知识没用介绍,感兴趣的童鞋,可以自己试试哦。




原标题:IOS 网络浅析-(六 网络图片获取之三方SDWebImage

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

吉茂跨境电商学校:https://www.goluckyvip.com/tag/34353.html
吉茂学校:https://www.goluckyvip.com/tag/34354.html
吉米聊跨境:https://www.goluckyvip.com/tag/34355.html
吉喵云:https://www.goluckyvip.com/tag/34356.html
吉特跨境:https://www.goluckyvip.com/tag/34358.html
吉祥邮:https://www.goluckyvip.com/tag/34359.html
北京到嵩山自驾游沿途景点 北京距离嵩山有多远:https://www.vstour.cn/a/411244.html
泰国签证有出生地 泰国出生入籍护照:https://www.vstour.cn/a/411245.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流