iOS下载网络图片,image总是为空

发布于 2021-11-27 14:07:03 字数 3247 浏览 408 评论 1

    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)


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流心雨 2021-11-28 03:30:18

格式化代码  快捷键  command + a  -> command + c -> command + v

你的线程间通信有问题.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文