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!
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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With