Browse files

Fixed an issue where the image view would be retained by the block. F…

…ixed an issue where a reused image view would have its current image ursupred by an earlier image that was still being downloaded when the new image was set.
  • Loading branch information...
1 parent dec0c5f commit c1e5da6ffe7593e9e4f015d329f579219fefbf53 Patrick Hughes committed Aug 31, 2012
Showing with 17 additions and 2 deletions.
  1. +17 −2 UIImageView+ZSAssetManagerAdditions.m
View
19 UIImageView+ZSAssetManagerAdditions.m
@@ -8,14 +8,29 @@
#import "UIImageView+ZSAssetManagerAdditions.h"
#import "ZSAssetManager.h"
+#import <objc/runtime.h>
+
+static char const * const assetManagerImageURLKey = "assetManagerImageURLKey";
@implementation UIImageView (ZSAssetManagerAdditions)
- (void)setImageWithURL:(NSURL *)url
{
+ if (!url) {
+ DLog(@"nil url passed");
+ objc_removeAssociatedObjects(self);
+ return;
+ }
+
+ objc_setAssociatedObject(self, assetManagerImageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+
+ __weak UIImageView *blockSelf = self;
[[ZSAssetManager sharedAssetManager] fetchImageForURL:url withCompletionBlock:^(NSURL *fetchedUrl, UIImage *image) {
- if ([url isEqual:fetchedUrl]) {
- self.image = image;
+
+ NSURL *currentURL = (NSURL*) objc_getAssociatedObject(self, assetManagerImageURLKey);
+
+ if ([currentURL isEqual:fetchedUrl]) {
+ blockSelf.image = image;
}
}];
}

0 comments on commit c1e5da6

Please sign in to comment.