美文网首页
用sdwebimge加载图片,异步执行

用sdwebimge加载图片,异步执行

作者: 逆夏的夏天0 | 来源:发表于2020-12-31 16:18 被阅读0次

    用sdwebimge加载图片,时候发现一个奇怪的问题:代码如下(A),发现无法重新绘制UIImageView

     UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.Postwidth, self.Postheight-64)];

        image.backgroundColor =[UIColor orangeColor];

        [imagesd_setImageWithURL:[NSURLURLWithString:self.urlstr]completed:^(UIImage*image,NSError*error,SDImageCacheTypecacheType,NSURL*imageURL) {

            if(!error) {

                 CGFloatimgW = image.size.width;

                 CGFloatimgH = image.size.height;

                 weakSelf.Postheight= (self.Postwidth/imgW)*imgH +64;

                 weakSelf.postview.frame=CGRectMake((SCREEN_WIDTH-self.Postwidth)/2,30,self.Postwidth,self.Postheight);

                 weakSelf.imgView.frame=CGRectMake(0,0,self.Postwidth,self.Postheight-64);

                 weakSelf.scoller.contentSize=CGSizeMake(self.Postwidth, weakSelf.Postheight+30);

            }

        }];

     self.imgView=image;

     [_postviewaddSubview:image];

    如果在回调里用主线程去重新绘制又是正常的,代码如下(B)

     UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.Postwidth, self.Postheight-64)];

        image.backgroundColor =[UIColor orangeColor];

        [imagesd_setImageWithURL:[NSURLURLWithString:self.urlstr]completed:^(UIImage*image,NSError*error,SDImageCacheTypecacheType,NSURL*imageURL) {

            if(!error) {

               dispatch_async(dispatch_get_main_queue(), ^{

                 CGFloatimgW = image.size.width;

                 CGFloatimgH = image.size.height;

                 weakSelf.Postheight= (self.Postwidth/imgW)*imgH +64;

                 weakSelf.postview.frame=CGRectMake((SCREEN_WIDTH-self.Postwidth)/2,30,self.Postwidth,self.Postheight);

                 weakSelf.imgView.frame=CGRectMake(0,0,self.Postwidth,self.Postheight-64);

                 weakSelf.scoller.contentSize=CGSizeMake(self.Postwidth, weakSelf.Postheight+30);

               });

            }

        }];

     self.imgView=image;

        [_postviewaddSubview:image];

    我猜测是不是回调不是是主线程,所以没有进行UI绘制,手动去设置回调里添加[weakSelf.imgView setNeedsLayout];

    结果无效。然后进行打断点,发现回调里self.imgView =nil;也就是是赋值没成功。尝试换了赋值的位置

    self.imgView=image;赋值代码提到imagesd_setImageWithURL:... 之前 具体如下(C)

    UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.Postwidth, self.Postheight-64)];

      self.imgView=image;

    //    [image sd_setImageWithURL:[NSURL URLWithString:self.urlstr]];

        image.backgroundColor =[UIColor orangeColor];

    [imagesd_setImageWithURL:[NSURLURLWithString:self.urlstr]completed:^(UIImage*image,NSError*error,SDImageCacheTypecacheType,NSURL*imageURL) {

    if(!error) {

    CGFloatimgW = image.size.width;

                 CGFloatimgH = image.size.height;

    weakSelf.Postheight= (self.Postwidth/imgW)*imgH +64;

    weakSelf.postview.frame=CGRectMake((SCREEN_WIDTH-self.Postwidth)/2,30,self.Postwidth,self.Postheight);

    weakSelf.imgView.frame=CGRectMake(0,0,self.Postwidth,self.Postheight-64);

    weakSelf.scoller.contentSize=CGSizeMake(self.Postwidth, weakSelf.Postheight+30);

            }

        }];

        [_postviewaddSubview:image];

    惊奇的发现又可以绘制了。于是有了这样的猜测:加载图片时候主线程开辟子线程时候打了个锚点,执行回调方法是回到了锚点位置,也就是主线程并未对 self.imgView赋值的位置,所以回调执行时 self.imgView是空的。而用获取主线程来执行是获取了主线程最新状态也就是self.imgView过赋值了,所以可以获取self.imgView进行重绘。

    图(图丑见谅)解如下:


    以上是我的猜测,仅供参考。欢迎指点纠正。

    相关文章

      网友评论

          本文标题:用sdwebimge加载图片,异步执行

          本文链接:https://www.haomeiwen.com/subject/icxroktx.html