Skip to content

Instantly share code, notes, and snippets.

@amleszk
Created June 22, 2016 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amleszk/a1359156e6b741b14963d5313dfdfb71 to your computer and use it in GitHub Desktop.
Save amleszk/a1359156e6b741b14963d5313dfdfb71 to your computer and use it in GitHub Desktop.
Fallback URL handling in SDWebImage
//
// UIImageView+RestaurantWebImage.h
// DinerApp
//
// Created by ALeszkiewicz on 6/21/16.
// Copyright © 2016 GrubHub Inc. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIImageView (RestaurantWebImage)
- (void)beginImageDownloadWithURLs:(NSArray <NSURL *> *)imageURLs;
@end
NS_ASSUME_NONNULL_END
//
// UIImageView+RestaurantWebImage.m
// DinerApp
//
// Created by ALeszkiewicz on 6/21/16.
// Copyright © 2016 GrubHub Inc. All rights reserved.
//
#import "UIImageView+RestaurantWebImage.h"
#import "GKConstants.h"
#import <SDWebImage/UIImageView+WebCache.h>
@implementation UIImageView (RestaurantWebImage)
- (void)beginImageDownloadWithURL:(NSURL * _Nonnull)imageURL {
UIImage *placeholderImageOnFailure;
if (IS_IPAD) {
placeholderImageOnFailure = [UIImage imageNamed:@"MenuHeaderRestaurantPlaceholder"];
} else {
placeholderImageOnFailure = [UIImage imageNamed:@"MenuHeaderRestaurantPlaceholder_iPhone"];
}
__weak __typeof(self)weakSelf = self;
[self sd_setImageWithURL:imageURL
placeholderImage:placeholderImageOnFailure
options:SDWebImageDelayPlaceholder
progress:nil
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[weakSelf didCompleteImageDownloadWithCacheType:cacheType];
}];
}
- (void)beginImageDownloadWithURLs:(NSArray <NSURL *> *)imageURLs {
self.image = nil;
NSURL *firstURL = [imageURLs firstObject];
if (imageURLs.count > 1) {
[self beginImageDownloadWithURL:firstURL fallbackURLsOnFailure:[imageURLs subarrayWithRange:NSMakeRange(1, imageURLs.count-1)]];
} else {
[self beginImageDownloadWithURL:firstURL];
}
}
- (void)beginImageDownloadWithURL:(NSURL * _Nonnull)imageURL fallbackURLsOnFailure:(NSArray <NSURL *> * _Nonnull)fallbackURLsOnFailure {
NSURL *nextURLOnFailure = [fallbackURLsOnFailure firstObject];
if (!nextURLOnFailure) {
[self beginImageDownloadWithURL:imageURL];
} else {
__weak __typeof(self)weakSelf = self;
[self sd_setImageWithURL:imageURL
placeholderImage:nil
options:0
progress:nil
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
__strong __typeof(self)strongSelf = weakSelf;
if (!strongSelf) {
return;
}
if (!image) {
//Pop first fallbackURL item, we know it's present because 'nextURLOnFailure' is non-nil
NSMutableArray *fallbackURLsMutable = [fallbackURLsOnFailure mutableCopy];
[fallbackURLsMutable removeObjectAtIndex:0];
[strongSelf beginImageDownloadWithURL:nextURLOnFailure
fallbackURLsOnFailure:[fallbackURLsMutable copy]];
} else {
[strongSelf didCompleteImageDownloadWithCacheType:cacheType];
}
}];
}
}
- (void)didCompleteImageDownloadWithCacheType:(SDImageCacheType)imageCacheType
{
BOOL hasImage = self.image != nil;
//Do not animate if the image was from cache, the user will not have time to see the animations
if (hasImage && imageCacheType == SDImageCacheTypeNone) {
[UIView transitionWithView:self
duration:0.2
options:UIViewAnimationOptionTransitionCrossDissolve
animations:nil
completion:nil];
}
self.accessibilityValue =
hasImage ?
GKAccessibilityString(@"Placeholder Image", @"Value", kSectionRestaurantHeader) :
GKAccessibilityString(@"Restaurant Logo", @"Value", kSectionRestaurantHeader);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment