理解SDWebImage下载器

时间:2015-08-11 18:56:44

标签: ios swift sdwebimage

文档并没有为我删除它。有人可以给我示例代码或实例化SDWebImage下载器并使用downloadImageWithURL方法的示例吗?

2 个答案:

答案 0 :(得分:6)

我为此创建了一个类,但这是我使用它的方式:

- (void)setImageURL:(NSURL *)imageURL {
    _imageURL = imageURL;
    self.image = nil;
    [self.layer removeAllAnimations];

    if (!imageURL) {
        self.image = self.placeHolderImage;
        return;
    }

    __weak SDImageView *weakself = self;

    self.progressView.hidden = NO;

    SDWebImageDownloaderProgressBlock progressBlock =
    ^(NSInteger receivedSize, NSInteger expectedSize) {
        if (expectedSize > 0) {
            float progress = receivedSize / (float)expectedSize;
            weakself.progressView.progress = MAX(MIN(1, progress), 0);
        }
    };

    SDWebImageCompletionWithFinishedBlock successBlock =
    ^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        if (!image) image = self.placeHolderImage;

        weakself.imageOperation = nil;
        weakself.progressView.hidden = YES;
        weakself.progressView.progress = 0.f;

        if (cacheType == SDImageCacheTypeNone && !error) {
            [UIView transitionWithView:self
                              duration:1.f
                               options:UIViewAnimationOptionTransitionCrossDissolve
                            animations:^{
                                weakself.image = image;
                            } completion:nil];
        } else {
            weakself.image = image;
        }
    };

    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    self.imageOperation = [manager downloadImageWithURL:imageURL
                                                options:SDWebImageRetryFailed
                                               progress:progressBlock
                                              completed:successBlock];
}

答案 1 :(得分:0)

你想做什么?给我们更多细节。

Stackoverflow:https://stackoverflow.com/a/27617962/846780

Github:https://github.com/ZuYuanZhou/testSD