【问题标题】:Image not loading for image图像未加载图像
【发布时间】:2017-07-01 10:07:14
【问题描述】:

我只想为UIButton 设置图像并为其创建IBOutlet。我上面有一个加载器,当加载图像时,加载器将停止并为该按钮分配一个新图像。调用代码时,占位符图片保留,不加载url图片

  [_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [_ImageLoader1 stopAnimating];
        [_ImageLoader1 removeFromSuperview];
    }];

【问题讨论】:

    标签: ios objective-c input-button-image


    【解决方案1】:

    您正在使用完成块从 url 下载图像。因此,您需要按照以下方式进行操作。

     [_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    
                [_Image1 setImage:btnImage forState:UIControlStateNormal];//Don't forgot to check for error if error is nil or image is not nil then only set image
    
                [_ImageLoader1 stopAnimating];
                [_ImageLoader1 removeFromSuperview];
      }];
    

    【讨论】:

    • 不能使用 '_Image1.image = image' 作为按钮
    【解决方案2】:

    如果您使用完成块 SDWebImageCache ,请在完成块内设置图像

    [_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            _Image1.image = image;
            [_ImageLoader1 stopAnimating];
            [_ImageLoader1 removeFromSuperview];
        }];
    

    【讨论】:

      【解决方案3】:

      在主线程中更新图像尝试这样的事情

       [_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
              [_ImageLoader1 stopAnimating];
              [_ImageLoader1 removeFromSuperview];
      
              dispatch_sync(dispatch_get_main_queue(), ^{
                 _Image1.image = image;   
              });
      
       }];
      

      【讨论】:

        【解决方案4】:

        NSUrlRequestNSUrl URLWithString“你的网址”获取NSData

        并将NSData 转换为UIImage 并设置背景图片。

        【讨论】:

          【解决方案5】:

          首先获取图片 url 并检查图片 url 长度是否大于零/不是null。并确保图像 url 是否正常工作。在Browser 上点击url 并检查您是否获得了正确的图像。

          SDWebImage 库正在更新按钮图像,但这在Main Thread 上没有发生。这可能是没有更新backgroundImageUIButton 的原因。

          - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
          forState:(UIControlState)state
          placeholderImage:(nullable UIImage *)placeholder
          options:(SDWebImageOptions)options
          completed:(nullable SDExternalCompletionBlock)completedBlock {
              if (!url) {
                  [self.imageURLStorage removeObjectForKey:@(state)];
                  return;
              }
          
              NSString *imageURL = [arrayGallery valueForKey:@"link"][0];
              self.imageURLStorage[@(state)] = url;
          
              __weak typeof(self)weakSelf = self;
              [self sd_internalSetImageWithURL:url
                              placeholderImage:placeholder
                                       options:options
                                  operationKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]
                                 setImageBlock:^(UIImage *image, NSData *imageData) {
                                     [weakSelf setBackgroundImage:image forState:state];
                                 }
                                      progress:nil
                                     completed:completedBlock];
          }
          

          停止loader 动画并在main thread 上设置UIButton 背景图像。

          NSString *imageURL = [arrayGallery valueForKey:@"link"][0];
          
          if (imageURL.length > 0) {
          
              [_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageURL] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                  dispatch_async(dispatch_get_main_queue(), ^{
                      [_ImageLoader1 stopAnimating];
                      [_ImageLoader1 removeFromSuperview];
                      [_Image1 setBackgroundImage:image forState:UIControlStateNormal];
                  });
              }];
          
          }
          

          【讨论】:

            猜你喜欢
            • 2021-10-03
            • 2012-09-20
            • 1970-01-01
            • 2013-12-10
            • 2016-09-28
            • 2021-07-12
            • 2019-07-24
            • 2015-01-30
            • 2020-10-07
            相关资源
            最近更新 更多