Created
October 25, 2013 23:42
-
-
Save priore/7163510 to your computer and use it in GitHub Desktop.
SDWebImage extended with alternate URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // SDWebImage author: @Olivier_Poitrey | |
| // source code https://github.com/rs/SDWebImage | |
| // | |
| // Extension author: @DaniloPriore | |
| // | |
| #import "SDWebImageCompat.h" | |
| #import "SDWebImageManager.h" | |
| #import "UIImageView+WebCache.h" | |
| #import "objc/runtime.h" | |
| @implementation UIImageView (AlternateURL) | |
| static char operationKey; | |
| - (void)setImageWithURL:(NSURL *)url | |
| alternateURL:(NSURL*)alternateURL | |
| placeholderImage:(UIImage *)placeholder | |
| completed:(SDWebImageCompletedBlock)completedBlock | |
| { | |
| [self cancelCurrentImageLoad]; | |
| if (url) | |
| { | |
| id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) | |
| { | |
| if (error) { | |
| NSLog(@"Image url: %@", [url absoluteString]); | |
| NSLog(@"Error loading: %@", [error localizedDescription]); | |
| [self setImageWithURL:alternateURL alternateURL:nil placeholderImage:placeholder completed:completedBlock]; | |
| } else { | |
| if (finished) { | |
| if (image) { | |
| self.image = image; | |
| } else { | |
| self.image = placeholder; | |
| } | |
| if (completedBlock) { | |
| completedBlock(image, error, cacheType); | |
| } | |
| } | |
| } | |
| }]; | |
| objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
| } else if (alternateURL) { | |
| [self setImageWithURL:alternateURL alternateURL:nil placeholderImage:placeholder completed:completedBlock]; | |
| } else { | |
| self.image = placeholder; | |
| if (completedBlock) { | |
| completedBlock(placeholder, nil, SDImageCacheTypeDisk); | |
| } | |
| } | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment