4
回答
iOS下载网络图片,image总是为空
华为云实践训练营,热门技术免费实践!>>>   

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSLog(@"donwload---%@", [NSThread currentThread]);

        // 1.子线程下载图片

        NSURL *url = [NSURL URLWithString:@"https://www.baidu.com/img/270new_2219485be6054791b9649fd0d423545f.png"];

        // 2.回到主线程设置图片

        dispatch_async(HMMainQueue, ^{

            UIImage *placeholder = [UIImage imageNamed:@"placeholder"];

            [self.button setImage:placeholder forState:UIControlStateNormal];

            SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageLowPriority;

            [self.image1 sd_setImageWithURL:url placeholderImage:placeholder options:options progress:^(NSInteger receivedSize, NSInteger expectedSize) {

                NSLog(@"下载进度:%f",  (double)receivedSize / expectedSize );

            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

                NSLog(@"----图片加载完毕---%@", image);

            }];

        });

    });

输出:

2016-03-08 14:12:27.666 09-GCD03-线程间的通信(掌握)[2536:93609] donwload---<NSThread: 0x7ff680d3cae0>{number = 2, name = (null)}

2016-03-08 14:12:27.684 09-GCD03-线程间的通信(掌握)[2536:93609] 下载进度:-0.000000

2016-03-08 14:12:27.778 09-GCD03-线程间的通信(掌握)[2536:93626] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

2016-03-08 14:12:27.780 09-GCD03-线程间的通信(掌握)[2536:93565] ----图片加载完毕---(null)






举报
zlzmkkmao
发帖于2年前 4回/478阅
顶部