前言:
这次在使用 SDWebImage 的时候,出现了加载图片无法显示的bug。找了一圈,发现是图片地址含有中文,然后SDWebImage就不买账。这时候我需要自己对图片地址编码,再交给SDWebImage才能正常加载,如果后端已经编码,我自己再次编码,那么,SDWebImage还是不会买账的。
这波需要从阿里云拿图片。这波就需要下载图片。然而这波的使用 SDWebImage 按照顺序递归下载图片的这份代码我最终还是没能用上。先记录一下吧。
1、效果图
2、上代码
#import "DownloadImageViewController.h"
#import <SDWebImage/SDWebImage.h>
@interface DownloadImageViewController ()
/// XIB 放了一个图片
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
/// XIB 放了一个进度条
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@end
@implementation DownloadImageViewController
- (void)dealloc {
NSLog(@"%s, %d, %s",__FILE_NAME__, __LINE__, __FUNCTION__);
}
- (void)viewDidLoad {
[super viewDidLoad];
//关于使用 SDWebImage 加载图片不显示, 有以下原因
/*
1.需要在 info.plist 中设置一个 YES
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsUsageDescription</key>
<dict/>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
2.如果是加载 https 地址开头的图片, 在调用SD的相关方法时候, 加上:
[options:SDWebImageDownloaderAllowInvalidSSLCertificates]
3.如果图片地址里有中文等其他语言的字符,也可能下载或加载失败, 这时候需要对url先编码:
NSString *encodeURL = [@"http://www.photo.com/东方不败.jpg" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
请注意, 使用输入法的时候, 半角情况下, 中文空格和英文空格编码相同(%20), 全角情况下,中文空格(%E3%80%80).
所以, BUG是, 这个图片是全英文命名,但是中间加入了一个中文全角空格,就贼坑.
*/
NSArray *arrayURLs = @[
@"https://desk-fd.zol-img.com.cn/t_s4096x2160c5/g2/M00/0B/07/ChMlWV5fCbqIZIdZABqMDTN-y4cAANjLQJIcC8AGowl707.jpg",
@"https://desk-fd.zol-img.com.cn/t_s4096x2160c5/g5/M00/00/0C/ChMkJl8IFJWIW4taAEdFfiR2z7YAAwzfQO1SmAAR0WW402.jpg",
@"https://desk-fd.zol-img.com.cn/t_s4096x2160c5/g5/M00/00/0C/ChMkJ18IF-uIc3IKACtn12hMV2oAAwzhwHVD8oAK2fv094.jpg",
@"https://desk-fd.zol-img.com.cn/t_s4096x2160c5/g5/M00/00/09/ChMkJl8H3ZqIEi61AC5KTYXg1PYAAwyrAFdg_8ALkpl282.jpg",
@"https://desk-fd.zol-img.com.cn/t_s4096x2160c5/g5/M00/0F/0A/ChMkJl8FLMGIH5vJADnC4IGxgYgAAwu2gGD2j4AOcL4500.jpg",
@"https://desk-fd.zol-img.com.cn/t_s4096x2160c5/g1/M00/04/07/ChMljV7_MyaIQPkQAFL6c1tU9hkAAQUUwFMb7wAUvqL573.jpg",
@"https://desk-fd.zol-img.com.cn/t_s4096x2160c5/g3/M06/03/00/ChMlWF7wIoSIBXgMAA2RnYAh5O0AAVFQQAhci8ADZG1018.jpg",
@"https://desk-fd.zol-img.com.cn/t_s4096x2160c5/g5/M00/08/05/ChMkJlwQ_02IGzDxAAovW7UFYOsAAtvVgMq0OoACi9z517.jpg",
@"http://pic.netbian.com/uploads/allimg/190504/220358-155697863862bc.jpg",
];
NSMutableArray *arrayImages = [NSMutableArray array];
[self downloadImage:arrayURLs arrayImages:arrayImages currentIndex:0 success:^(NSArray<UIImage *> *resultImages) {
NSLog(@"下载成功!");
NSLog(@"%@", resultImages); // arrayImages 和 resultImages 地址一样
} failure:^{
NSLog(@"下载失败!");
}];
}
//递归按序下载图片
- (void)downloadImage:(NSArray <NSString *> *)arrayURLs arrayImages:(NSMutableArray<UIImage *> *)arrayImages currentIndex:(NSUInteger)index success:(void (^)(NSArray <UIImage *> *resultImages))success failure:(void (^)(void))failure {
__block NSUInteger currentIndex = index;
//这里使用 weakself, 在控制器销毁的时候, 当前下载任务完成后,就不会继续下一个任务, 如果使用 self, 那么当控制器销毁的时候, SD 会继续下载直到所有任务完成
__weak typeof(self) weakSelf = self;
NSString *stringurl = arrayURLs[currentIndex];
NSURL *url = [NSURL URLWithString:stringurl];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url options:SDWebImageDownloaderIgnoreCachedResponse progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
float p = receivedSize / (expectedSize * 1.0);
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.progressView.progress = p;
});
NSLog(@"当前索引 %lu, 当前进度: %f", (unsigned long)currentIndex, p);
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
currentIndex++;
if (finished && !error) { //下载结束并且没有出错
[arrayImages addObject:image];
weakSelf.imageView.image = image;
}
if (currentIndex == arrayURLs.count) { //停止递归下载
if(arrayImages.count > 0){ //当数组有元素存在,认为是成功, 也可以只写一个回调block, 不区分成功与失败
if(success){
success(arrayImages);
}
}else{
if(failure){
failure();
}
}
return;
} else {
//继续递归下载
[weakSelf downloadImage:arrayURLs arrayImages:arrayImages currentIndex:currentIndex success:success failure:failure];
}
}];
}
@end