0

I just want to set image for a UIButton and created an IBOutlet for it. I have a loader above it, when the images are loaded then loader will stop and will assign a new image for that button. When code is called, the placeholder images remains and url images are not loaded

  [_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];
    }];
Subramanian P
  • 4,365
  • 2
  • 21
  • 25
meowsush
  • 187
  • 2
  • 15

5 Answers5

1

You are using completion block to download image from url. So you need to do as per below.

 [_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];
  }];
vivek bhoraniya
  • 1,526
  • 2
  • 17
  • 36
0

If you are using a completion block for SDWebImageCache ,Set image inside the completion block

[_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];
    }];
Amal T S
  • 3,327
  • 2
  • 24
  • 57
0

update image in main thread try something like this

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

 }];
Dharma
  • 3,007
  • 3
  • 23
  • 38
0

get NSData from NSUrlRequest and from NSUrl URLWithString "your url"

and convert NSData to UIImage and set background image.

iUser
  • 330
  • 2
  • 8
vaibby
  • 1,255
  • 1
  • 9
  • 23
0

First get the image url and check the image url length is greater than zero / not null. And make sure the image url is working correctly or not. Hit the url on Browser and check are you getting proper image.

SDWebImage library is updating the button image, But that's not happening on Main Thread. This might be the reason for not updating backgroundImage of the UIButton.

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

Stop loader animation and set UIButton background image on main thread.

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

}
Subramanian P
  • 4,365
  • 2
  • 21
  • 25