2017-06-30 117 views
0

我只想設置圖像爲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]; 
    }]; 

回答

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]; 
    }]; 
+0

不能使用'_Image1.image =圖像'作爲按鈕 – meowsush

+0

@meowsush檢查我編輯的答案,如果它的工作標記爲接受的答案 – vivek

0

如果您使用的是完成塊爲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]; 
    }]; 
0

更新映像嘗試像這樣

[_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; 
     }); 

}]; 
0

NSUrlRequest得到NSDataNSUrl URLWithString 「你的網址」

,並轉換到NSDataUIImage設置背景圖片。

0

首先得到圖片的url並檢查圖片的url長度是否大於零/不是null。並確保圖像網址工作正常與否。點擊Browser上的url,然後檢查您是否獲得了正確的圖像。

SDWebImage庫正在更新按鈕圖像,但這不會發生在Main Thread上。這可能是不更新UIButtonbackgroundImage的原因。

- (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]; 
     }); 
    }]; 

}