Objective-C Tips & Trick - Prioregroup.com
[Objective-C] SDWebImage extended with alternate URL
       
back

//
// 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






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com