4

The docs aren't cutting it for me. Can someone give me sample code or an example of instantiating the SDWebImage downloader and using the downloadImageWithURL method?

Grant Park
  • 1,004
  • 1
  • 9
  • 29

2 Answers2

6

I've created a class for that, but here's how I use it:

- (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];
}
Renan Kosicki
  • 2,890
  • 2
  • 29
  • 32
0

What you trying to do? Give us more details.

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

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

Community
  • 1
  • 1
Klevison
  • 3,342
  • 2
  • 19
  • 32
  • how can i upload images with SDImages? can i have only options for uploading image is AFNetworking? – ios developer Aug 31 '16 at 07:08
  • @iosdeveloper SDWebImage is an "Asynchronous image downloader with cache support as a UIImageView category". I doesn't upload images. To upload image you can use many other libs (such as AFNetworking). – Klevison Aug 31 '16 at 15:17