SDWebImage 完成块

SDWebImage completion Block

我正在使用 SDWebImage 代码从 Internet 下载图像。 GitHub SDWebImage 我在 Xcode 8 和 Swift 3 中这样做。

我想在下载后调整它的大小,但是在我下载图片之前我无法设置图片大小。 (使其按比例调整大小)

所以,我的问题是,有没有办法让完成块在图像下载完成后执行一些代码?我似乎无法在 github 项目中找到有关下载完成的信息。

谢谢!

下面是 SDWebImage 示例文件中的一些代码,您可以在此处找到更多示例:https://github.com/rs/SDWebImage/tree/master/Examples

Swift 版本:

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

Objective-C版本:

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

希望对您有所帮助。

我找到了! Post 以防其他人需要它。

我用它从网络加载

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

但是为了加载并完成一个块,我需要做的是:

 imgView.sd_setImage(with: URL(string: news.imageURL)) { (image, error, cache, url) in
            // Your code inside completion block
        }