Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDWebImage completion Block

Im using SDWebImage code, to download an image from internet. GitHub SDWebImage Im Doing this in Xcode 8 and Swift 3.

I want to resize it once it is downloaded, but I can't have image size until I Download the image. (To make it resize proportionally)

So, my question is, is there a way to have a completion block to execute some code when the image has finish being downloaded? I can`t seem to find on github project info about the completion of the download.

Thanks!

like image 955
Mago Nicolas Palacios Avatar asked Dec 30 '16 16:12

Mago Nicolas Palacios


2 Answers

I Found it! Post it in case someone else needs it.

I used this to load from web

imgView.sd_setImage(with: URL(string: news.imageURL))

But for load and have a completing block I need to do:

 imgView.sd_setImage(with: URL(string: news.imageURL)) { (image, error, cache, url) in
            // Your code inside completion block
        }
like image 115
Mago Nicolas Palacios Avatar answered Nov 02 '22 17:11

Mago Nicolas Palacios


Here's some code right out of one of the SDWebImage example files, you can find more examples here: https://github.com/rs/SDWebImage/tree/master/Examples

Swift Version:

imageView.sd_setImageWithURL(NSURL(string: urlString), completed: {
                (image, error, cacheType, url) in
                // your code
            })

Objective-C Version:

- (void)configureView
{
    if (self.imageURL) {
        __block UIActivityIndicatorView *activityIndicator;
        __weak UIImageView *weakImageView = self.imageView;
        [self.imageView sd_setImageWithURL:self.imageURL
                          placeholderImage:nil
                                   options:SDWebImageProgressiveDownload
                                  progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL *targetURL) {
                                      if (!activityIndicator) {
                                          [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                                          activityIndicator.center = weakImageView.center;
                                          [activityIndicator startAnimating];
                                      }
                                  }
                                 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                     [activityIndicator removeFromSuperview];
                                     activityIndicator = nil;
                                 }];
    }
}

Hope this helps.

like image 29
nenchev Avatar answered Nov 02 '22 17:11

nenchev