SDWebImage完成块

Mag*_*ios 1 ios sdwebimage swift3

我使用SDWebImage代码,从互联网上下载图像. GitHub SDWebImage Im在Xcode 8和Swift 3中执行此操作.

我想在下载后重新调整大小,但在下载图像之前我无法获得图像大小.(按比例调整大小)

所以,我的问题是,有一种方法可以在完成图像下载后使用完成块来执行某些代码吗?我似乎无法在github上找到关于下载完成的项目信息.

谢谢!

nen*_*hev 6

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

Swift版本:

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

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;
                                 }];
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


Mag*_*ios 5

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

我用它从网上加载

imgView.sd_setImage(with: URL(string: news.imageURL))
Run Code Online (Sandbox Code Playgroud)

但是对于负载并有一个完成块,我需要做:

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