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?
Asked
Active
Viewed 1,158 times
2 Answers
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
-
1Awesome. This is what I wanted to see. – Grant Park Aug 11 '15 at 19:10
-
what if i want to download image from web and store it in file manager for further use? – ios developer Aug 31 '16 at 07:06
0
What you trying to do? Give us more details.
Stackoverflow: https://stackoverflow.com/a/27617962/846780
-
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