SDWebImage完成块

5
我正在使用SDWebImage代码从互联网下载图像。 GitHub SDWebImage 我在Xcode 8和Swift 3中进行操作。
我想要在下载后调整图像大小,但是在下载图像之前无法得到图像大小。(为了使其按比例调整大小)
所以,我的问题是,有没有一种方法可以有一个完成块,在图像下载完成时执行一些代码?我似乎在github项目信息中找不到关于下载完成的信息。
谢谢!
2个回答

10

我找到了它!为了让其他需要的人也能使用,我将其发布了。

我用这个加载网页内容

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
        }

完成块帮助我。 - coders

9

以下是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;
                                 }];
    }
}

希望这可以帮到您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接