SDWebImage using the image converted into an array of arrays imageStr

Code

//封装
+ (void)fetchCacheImgs:(NSArray *)arrImgStr finishBlk:(void(^)(NSArray *arrImgs))finishBlk{
    NSMutableArray *arrMTemp = [NSMutableArray arrayWithArray:arrImgStr];
    __block NSInteger sum = 0;
    [arrImgStr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *strImg = obj;
        [[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:strImg] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
            sum++;
            [arrMTemp replaceObjectAtIndex:idx withObject:image];
            if (sum == arrImgStr.count) {
                //结束
                if (finishBlk) {
                    finishBlk(arrMTemp);
                }
            }
        }];
    }];
}

//调用
[UIImage fetchCacheImgs:@[
                              @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558086203027&di=e27944d7d427004a71c2cdb0fbf58125&imgtype=0&src=http%3A%2F%2Fpic139.nipic.com%2Ffile%2F20170819%2F19317778_203257969000_2.jpg",
                              @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558086203027&di=83035960a9ac35ee59c90d277e29b484&imgtype=0&src=http%3A%2F%2Fs1.sinaimg.cn%2Fmw690%2F0021szcogy6VoIyTEw8d0%26690",
                              @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558086203027&di=5e307af14e64026034b3bf77ac423c21&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fbf389549d64d6853870dbda4e811a0f56ca138aaa424-lj2LaA_fw658",
                              @"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1792894298,856132472&fm=26&gp=0.jpg"
                              ] finishBlk:^(NSArray *arrImgs) {
                                  self.vm.arrMImgCountImg = [NSMutableArray arrayWithArray:arrImgs];
                              }];
复制代码

problem

answer

In fact, this block and mechanisms of a relationship. block There are two ways to access external variables.

One is the value passed is the address of another transfer. The front of the variable added __block modifier, it becomes a delivery address. This time block internal and external shared a variable address so you can change the value of an external variable.

There is also a transfer value. Is the value of an external variable transmission block will block access to the interior, with the final structure to save the value.

Let us analyze the code above under the
first external array traversal, idx = 0 to 0 this time block incoming internal use structure preserved. Then open multi-threaded, then after idx = 1 this time to save the block inside pass 1 with the structure to open up a thread. The final array traversal is complete, open multiple threads. This time the picture data and some large, some small amount of data. Therefore, although the first open thread is not necessarily to request complete data. So here it comes to multi-thread scheduling problem.

For example, the second open thread picture data is relatively small, the first request is completed, the first to enter the block this time passed before the value of idx 1 to play a role, will take idx block in the internal structure of the body to go, taking the value is on times the value passed is 1. We use a flexible array of characteristics corresponding image replacement string into a real picture can be.

prove

We rewrite the for loop, the loop variable front with __block to modify. Let it become a site transfer, we'll give it a try, you will find the results changed.

We can try to get rid of __block modifier run again.

This also proves the one hand the characteristics of the block, the value of the transfer, the transfer site.

On the other hand it can be viewed cpp file, by looking at the way the code can also illustrate this point.

to sum up

In fact, the above code is the use of function loadImageWithURL SDWebImage to complete the picture string converted into picture data. Here we will go to find the cache, directly back later to find, can not find it to download.

Digress from the use of SDWebImage complete strImg Image turn into a telling block properties. But I think this place is worthy of our attention. It is easy to overlook a knowledge point.

Today think about is mainly the use of the block. We take advantage of the characteristics of the external value of the variable block access to complete a "thread sort."

Reproduced in: https: //juejin.im/post/5cf0f34be51d45554877a578

Guess you like

Origin blog.csdn.net/weixin_33943836/article/details/91465682