From 654a9c98ea027a9f477598f943fbe37e3c31b4f6 Mon Sep 17 00:00:00 2001 From: Bogdan Poplauschi Date: Thu, 19 Jun 2014 11:32:28 +0300 Subject: [PATCH] Pass image URL in completion blocks - step 2: - created block type `SDWebImageCompletionBlock` that contains NSURL* param - deprecated all MKAnnotationView(WebCache) `setImage*` methods. Replaced with `loadImage*` methods that use the `SDWebImageCompletionBlock` as completion block type - created WebCacheDeprecated category on MKAnnotationView (to avoid collisions, we didn't name it Deprecated) - replaced the usages of the deprecated items with the new ones --- SDWebImage/MKAnnotationView+WebCache.h | 29 ++++++++--- SDWebImage/MKAnnotationView+WebCache.m | 68 ++++++++++++++++++++------ SDWebImage/SDWebImageManager.h | 3 +- 3 files changed, 77 insertions(+), 23 deletions(-) diff --git a/SDWebImage/MKAnnotationView+WebCache.h b/SDWebImage/MKAnnotationView+WebCache.h index 2be81c5b..724f1a86 100644 --- a/SDWebImage/MKAnnotationView+WebCache.h +++ b/SDWebImage/MKAnnotationView+WebCache.h @@ -18,7 +18,7 @@ * Get the current image URL. * * Note that because of the limitations of categories this property can get out of sync - * if you use setImage: directly. + * if you use loadImage: directly. */ - (NSURL *)imageURL; @@ -29,7 +29,7 @@ * * @param url The url for the image. */ -- (void)setImageWithURL:(NSURL *)url; +- (void)loadImageWithURL:(NSURL *)url; /** * Set the imageView `image` with an `url` and a placeholder. @@ -38,9 +38,9 @@ * * @param url The url for the image. * @param placeholder The image to be set initially, until the image request finishes. - * @see setImageWithURL:placeholderImage:options: + * @see loadImageWithURL:placeholderImage:options: */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder; +- (void)loadImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder; /** * Set the imageView `image` with an `url`, placeholder and custom options. @@ -51,7 +51,7 @@ * @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; +- (void)loadImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options; /** * Set the imageView `image` with an `url`. @@ -64,7 +64,7 @@ * 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; +- (void)loadImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock; /** * Set the imageView `image` with an `url`, placeholder. @@ -78,7 +78,7 @@ * 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; +- (void)loadImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock; /** * Set the imageView `image` with an `url`, placeholder and custom options. @@ -93,7 +93,7 @@ * 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; +- (void)loadImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock; /** * Cancel the current download @@ -101,3 +101,16 @@ - (void)cancelCurrentImageLoad; @end + + +@interface MKAnnotationView (WebCacheDeprecated) + +- (void)setImageWithURL:(NSURL *)url __deprecated_msg("Method deprecated. Use `loadImageWithURL:`"); +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder __deprecated_msg("Method deprecated. Use `loadImageWithURL:placeholderImage:`"); +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options __deprecated_msg("Method deprecated. Use `loadImageWithURL:placeholderImage:options:`"); + +- (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `loadImageWithURL:completed:`"); +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `loadImageWithURL:placeholderImage:completed:`"); +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `loadImageWithURL:placeholderImage:options:completed:`"); + +@end diff --git a/SDWebImage/MKAnnotationView+WebCache.m b/SDWebImage/MKAnnotationView+WebCache.m index 2a29235d..88166566 100644 --- a/SDWebImage/MKAnnotationView+WebCache.m +++ b/SDWebImage/MKAnnotationView+WebCache.m @@ -14,33 +14,32 @@ static char operationKey; @implementation MKAnnotationView (WebCache) -- (NSURL *)imageURL; -{ +- (NSURL *)imageURL { return objc_getAssociatedObject(self, &imageURLKey); } -- (void)setImageWithURL:(NSURL *)url +- (void)loadImageWithURL:(NSURL *)url { - [self setImageWithURL:url placeholderImage:nil options:0 completed:nil]; + [self loadImageWithURL:url placeholderImage:nil options:0 completed:nil]; } -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder { - [self setImageWithURL:url placeholderImage:placeholder options:0 completed:nil]; +- (void)loadImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder { + [self loadImageWithURL:url placeholderImage:placeholder options:0 completed:nil]; } -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options { - [self setImageWithURL:url placeholderImage:placeholder options:options completed:nil]; +- (void)loadImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options { + [self loadImageWithURL:url placeholderImage:placeholder options:options completed:nil]; } -- (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock { - [self setImageWithURL:url placeholderImage:nil options:0 completed:completedBlock]; +- (void)loadImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock { + [self loadImageWithURL:url placeholderImage:nil options:0 completed:completedBlock]; } -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock { - [self setImageWithURL:url placeholderImage:placeholder options:0 completed:completedBlock]; +- (void)loadImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock { + [self loadImageWithURL:url placeholderImage:placeholder options:0 completed:completedBlock]; } -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { +- (void)loadImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock { [self cancelCurrentImageLoad]; objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC); @@ -57,7 +56,7 @@ static char operationKey; sself.image = image; } if (completedBlock && finished) { - completedBlock(image, error, cacheType); + completedBlock(image, error, cacheType, url); } }); }]; @@ -75,3 +74,44 @@ static char operationKey; } @end + + +@implementation MKAnnotationView (WebCacheDeprecated) + +- (void)setImageWithURL:(NSURL *)url { + [self loadImageWithURL:url placeholderImage:nil options:0 completed:nil]; +} + +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder { + [self loadImageWithURL:url placeholderImage:placeholder options:0 completed:nil]; +} + +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options { + [self loadImageWithURL:url placeholderImage:placeholder options:options completed:nil]; +} + +- (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock { + [self loadImageWithURL:url placeholderImage:nil options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { + if (completedBlock) { + completedBlock(image, error, cacheType); + } + }]; +} + +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock { + [self loadImageWithURL:url placeholderImage:placeholder options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { + if (completedBlock) { + completedBlock(image, error, cacheType); + } + }]; +} + +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { + [self loadImageWithURL:url placeholderImage:placeholder options:options completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { + if (completedBlock) { + completedBlock(image, error, cacheType); + } + }]; +} + +@end diff --git a/SDWebImage/SDWebImageManager.h b/SDWebImage/SDWebImageManager.h index 3aa97836..e40ceeca 100644 --- a/SDWebImage/SDWebImageManager.h +++ b/SDWebImage/SDWebImageManager.h @@ -77,7 +77,7 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) { SDWebImageDelayPlaceholder = 1 << 9 }; -typedef void(^SDWebImageCompletedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType); +typedef void(^SDWebImageCompletionBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL); typedef void(^SDWebImageCompletionWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL); @@ -229,6 +229,7 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager]; #pragma mark - Deprecated +typedef void(^SDWebImageCompletedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType); typedef void(^SDWebImageCompletedWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) __deprecated_msg("Block type deprecated. Use `SDWebImageCompletionWithFinishedBlock`");