diff --git a/SDWebImage/SDWebImageCompat.h b/SDWebImage/SDWebImageCompat.h index cf51761b2..cabeaa674 100644 --- a/SDWebImage/SDWebImageCompat.h +++ b/SDWebImage/SDWebImageCompat.h @@ -13,10 +13,6 @@ #error SDWebImage does not support Objective-C Garbage Collection #endif -#if !__has_feature(objc_arc) -#error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag -#endif - #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0 #error SDWebImage doesn't support Deployement Target version < 5.0 #endif @@ -57,43 +53,4 @@ #define SDDispatchQueueSetterSementics assign #endif -NS_INLINE UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData) -{ - if (!imageOrData) - { - return nil; - } - - UIImage *image = nil; - if ([imageOrData isKindOfClass:[NSData class]]) - { - image = [[UIImage alloc] initWithData:(NSData *)imageOrData]; - } - else if ([imageOrData isKindOfClass:[UIImage class]]) - { - image = (UIImage *)imageOrData; - } - else - { - return nil; - } - - if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) - { - CGFloat scale = 1.0; - if (path.length >= 8) - { - // Search @2x. at the end of the string, before a 3 to 4 extension length (only if key len is 8 or more @2x. + 4 len ext) - NSRange range = [path rangeOfString:@"@2x." options:0 range:NSMakeRange(path.length - 8, 5)]; - if (range.location != NSNotFound) - { - scale = 2.0; - } - } - - UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation]; - image = scaledImage; - } - - return image; -} +extern UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData); diff --git a/SDWebImage/SDWebImageCompat.m b/SDWebImage/SDWebImageCompat.m new file mode 100644 index 000000000..2003415cf --- /dev/null +++ b/SDWebImage/SDWebImageCompat.m @@ -0,0 +1,46 @@ +#import "SDWebImageCompat.h" + +#if !__has_feature(objc_arc) +#error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag +#endif + +UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData) +{ + if (!imageOrData) + { + return nil; + } + + UIImage *image = nil; + if ([imageOrData isKindOfClass:[NSData class]]) + { + image = [[UIImage alloc] initWithData:(NSData *)imageOrData]; + } + else if ([imageOrData isKindOfClass:[UIImage class]]) + { + image = (UIImage *)imageOrData; + } + else + { + return nil; + } + + if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) + { + CGFloat scale = 1.0; + if (path.length >= 8) + { + // Search @2x. at the end of the string, before a 3 to 4 extension length (only if key len is 8 or more @2x. + 4 len ext) + NSRange range = [path rangeOfString:@"@2x." options:0 range:NSMakeRange(path.length - 8, 5)]; + if (range.location != NSNotFound) + { + scale = 2.0; + } + } + + UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation]; + image = scaledImage; + } + + return image; +} \ No newline at end of file diff --git a/SDWebImage/SDWebImageManager.h b/SDWebImage/SDWebImageManager.h index 9c7a753f9..a7f513f08 100644 --- a/SDWebImage/SDWebImageManager.h +++ b/SDWebImage/SDWebImageManager.h @@ -61,6 +61,8 @@ typedef void(^SDWebImageCompletedWithFinishedBlock)(UIImage *image, NSError *err */ @interface SDWebImageManager : NSObject +- (id)initWithImageCache: (SDImageCache *)cache; + @property (strong, nonatomic, readonly) SDImageCache *imageCache; @property (strong, nonatomic, readonly) SDWebImageDownloader *imageDownloader; diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index ca86f768b..ea241f85b 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -36,10 +36,15 @@ + (id)sharedManager } - (id)init +{ + return [self initWithImageCache: nil]; +} + +- (id)initWithImageCache: (SDImageCache *)cache { if ((self = [super init])) { - _imageCache = SDImageCache.new; + _imageCache = SSValueOrFallback(cache, [SDImageCache new]); _imageDownloader = SDWebImageDownloader.new; _failedURLs = NSMutableArray.new; _runningOperations = NSMutableArray.new; @@ -47,7 +52,6 @@ - (id)init return self; } - - (NSString *)cacheKeyForURL:(NSURL *)url { if (self.cacheKeyFilter) diff --git a/SDWebImage/UIImageView+WebCache.h b/SDWebImage/UIImageView+WebCache.h index ed6427ac8..86cbcad57 100644 --- a/SDWebImage/UIImageView+WebCache.h +++ b/SDWebImage/UIImageView+WebCache.h @@ -44,97 +44,125 @@ @interface UIImageView (WebCache) /** - * Set the imageView `image` with an `url`. - * - * The downloand is asynchronous and cached. - * - * @param url The url for the image. - */ -- (void)setImageWithURL:(NSURL *)url; - -/** - * Set the imageView `image` with an `url` and a placeholder. - * - * The downloand is asynchronous and cached. - * - * @param url The url for the image. - * @param placeholder The image to be set initially, until the image request finishes. - * @see setImageWithURL:placeholderImage:options: - */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder; - -/** - * Set the imageView `image` with an `url`, placeholder and custom options. - * - * The downloand is asynchronous and cached. - * - * @param url The url for the image. - * @param placeholder The image to be set initially, until the image request finishes. - * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. - */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options; - -/** - * Set the imageView `image` with an `url`. - * - * The downloand is asynchronous and cached. - * - * @param url The url for the image. - * @param completedBlock A block called when operation has been completed. This block as no return value - * and takes the requested UIImage as first parameter. In case of error the image parameter - * is nil and the second parameter may contain an NSError. The third parameter is a Boolean - * indicating if the image was retrived from the local cache of from the network. + * Cancel the current download */ -- (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock; +- (void)cancelCurrentImageLoad; -/** - * Set the imageView `image` with an `url`, placeholder. - * - * The downloand is asynchronous and cached. - * - * @param url The url for the image. - * @param placeholder The image to be set initially, until the image request finishes. - * @param completedBlock A block called when operation has been completed. This block as no return value - * and takes the requested UIImage as first parameter. In case of error the image parameter - * is nil and the second parameter may contain an NSError. The third parameter is a Boolean - * indicating if the image was retrived from the local cache of from the network. - */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock; +/* ###################################################################### */ +/* ###################################################################### */ +/* ###################################################################### */ -/** - * Set the imageView `image` with an `url`, placeholder and custom options. - * - * The downloand is asynchronous and cached. - * - * @param url The url for the image. - * @param placeholder The image to be set initially, until the image request finishes. - * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. - * @param completedBlock A block called when operation has been completed. This block as no return value - * and takes the requested UIImage as first parameter. In case of error the image parameter - * is nil and the second parameter may contain an NSError. The third parameter is a Boolean - * indicating if the image was retrived from the local cache of from the network. - */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock; +/* I've commented-out these methods so that we get a compiler warning if we try to use them; + use the methods in UIImageView+LOCaching instead, so the image download uses the correct cache! -DK */ -/** - * Set the imageView `image` with an `url`, placeholder and custom options. - * - * The downloand is asynchronous and cached. - * - * @param url The url for the image. - * @param placeholder The image to be set initially, until the image request finishes. - * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. - * @param progressBlock A block called while image is downloading - * @param completedBlock A block called when operation has been completed. This block as no return value - * and takes the requested UIImage as first parameter. In case of error the image parameter - * is nil and the second parameter may contain an NSError. The third parameter is a Boolean - * indicating if the image was retrived from the local cache of from the network. - */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock; +/* ###################################################################### */ +/* ###################################################################### */ +/* ###################################################################### */ -/** - * Cancel the current download - */ -- (void)cancelCurrentImageLoad; +///** +// * Set the imageView `image` with an `url`. +// * +// * The downloand is asynchronous and cached. +// * +// * @param url The url for the image. +// */ +//- (void)setImageWithURL:(NSURL *)url; +// +///** +// * Set the imageView `image` with an `url` and a placeholder. +// * +// * The downloand is asynchronous and cached. +// * +// * @param url The url for the image. +// * @param placeholder The image to be set initially, until the image request finishes. +// * @see setImageWithURL:placeholderImage:options: +// */ +//- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder; +// +///** +// * Set the imageView `image` with an `url`, placeholder and custom options. +// * +// * The downloand is asynchronous and cached. +// * +// * @param url The url for the image. +// * @param placeholder The image to be set initially, until the image request finishes. +// * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. +// */ +//- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options; +// +///** +// * Set the imageView `image` with an `url`. +// * +// * The downloand is asynchronous and cached. +// * +// * @param url The url for the image. +// * @param completedBlock A block called when operation has been completed. This block as no return value +// * and takes the requested UIImage as first parameter. In case of error the image parameter +// * is nil and the second parameter may contain an NSError. The third parameter is a Boolean +// * indicating if the image was retrived from the local cache of from the network. +// */ +//- (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock; +// +///** +// * Set the imageView `image` with an `url`, placeholder. +// * +// * The downloand is asynchronous and cached. +// * +// * @param url The url for the image. +// * @param placeholder The image to be set initially, until the image request finishes. +// * @param completedBlock A block called when operation has been completed. This block as no return value +// * and takes the requested UIImage as first parameter. In case of error the image parameter +// * is nil and the second parameter may contain an NSError. The third parameter is a Boolean +// * indicating if the image was retrived from the local cache of from the network. +// */ +//- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock; +// +///** +// * Set the imageView `image` with an `url`, placeholder and custom options. +// * +// * The downloand is asynchronous and cached. +// * +// * @param url The url for the image. +// * @param placeholder The image to be set initially, until the image request finishes. +// * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. +// * @param completedBlock A block called when operation has been completed. This block as no return value +// * and takes the requested UIImage as first parameter. In case of error the image parameter +// * is nil and the second parameter may contain an NSError. The third parameter is a Boolean +// * indicating if the image was retrived from the local cache of from the network. +// */ +//- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock; +// +///** +// * Set the imageView `image` with an `url`, placeholder and custom options. +// * +// * The downloand is asynchronous and cached. +// * +// * @param url The url for the image. +// * @param placeholder The image to be set initially, until the image request finishes. +// * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. +// * @param progressBlock A block called while image is downloading +// * @param completedBlock A block called when operation has been completed. This block as no return value +// * and takes the requested UIImage as first parameter. In case of error the image parameter +// * is nil and the second parameter may contain an NSError. The third parameter is a Boolean +// * indicating if the image was retrived from the local cache of from the network. +// */ +//- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock; +// +///** +// * Set the imageView `image` with an `url`, placeholder and custom options. +// * +// * The downloand is asynchronous and cached. +// * +// * @param url The url for the image. +// * @param placeholder The image to be set initially, until the image request finishes. +// * @param manager The manager with which to download and cache the image. If nil, assumed to be +[SDWebImageManager sharedManager]. +// * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. +// * @param progressBlock A block called while image is downloading +// * @param completedBlock A block called when operation has been completed. This block as no return value +// * and takes the requested UIImage as first parameter. In case of error the image parameter +// * is nil and the second parameter may contain an NSError. The third parameter is a Boolean +// * indicating if the image was retrived from the local cache of from the network. +// */ +//- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder manager:(SDWebImageManager *)manager options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock; @end diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index 01eab545b..44c5e403c 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -15,35 +15,40 @@ @implementation UIImageView (WebCache) - (void)setImageWithURL:(NSURL *)url { - [self setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil]; + [self setImageWithURL:url placeholderImage:nil manager:nil options:0 progress:nil completed:nil]; } - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder { - [self setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil]; + [self setImageWithURL:url placeholderImage:placeholder manager:nil options:0 progress:nil completed:nil]; } - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options { - [self setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil]; + [self setImageWithURL:url placeholderImage:placeholder manager:nil options:options progress:nil completed:nil]; } - (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock { - [self setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock]; + [self setImageWithURL:url placeholderImage:nil manager:nil options:0 progress:nil completed:completedBlock]; } - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock { - [self setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock]; + [self setImageWithURL:url placeholderImage:placeholder manager:nil options:0 progress:nil completed:completedBlock]; } - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { - [self setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock]; + [self setImageWithURL:url placeholderImage:placeholder manager:nil options:options progress:nil completed:completedBlock]; } - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock; +{ + [self setImageWithURL:url placeholderImage:placeholder manager:nil options:options progress:nil completed:completedBlock]; +} + +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder manager:(SDWebImageManager *)manager options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock { [self cancelCurrentImageLoad]; @@ -52,7 +57,11 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt if (url) { __weak UIImageView *wself = self; - id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) + + if (!manager) + manager = [SDWebImageManager sharedManager]; + + id operation = [manager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { __strong UIImageView *sself = wself; if (!sself) return;