Skip to content

Commit

Permalink
Added Default images to Demo app. Improved image loading behavior. Ad…
Browse files Browse the repository at this point in the history
…ded a note about the call to removeAllObjects in the init method of DemoViewController.
  • Loading branch information
jakemarsh committed Dec 3, 2012
1 parent 81e3cb8 commit 229b10d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
17 changes: 11 additions & 6 deletions Demo/Classes/DemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "DemoViewController.h"
#import "DemoTableViewCell.h"
#import "JMImageCache.h"

@interface DemoViewController ()

Expand Down Expand Up @@ -35,6 +35,9 @@ - (id) init {
[self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"https://cl.ly/4iNI/Untitled-7.png", @"ImageURL", @"Kevin Malone", @"Title", nil]];
[self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"https://cl.ly/4iAX/Untitled-7.png", @"ImageURL", @"Stanley Hudson", @"Title", nil]];

// You should remove this next line from your apps!!!
// It is only here for demonstration purposes, so you can get an idea for what it's like to load images "fresh" for the first time.

[[JMImageCache sharedCache] removeAllObjects];

return self;
Expand All @@ -61,16 +64,18 @@ - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSIntege
return [self.modelArray count];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DemoTableViewCell *cell = (DemoTableViewCell* )[tableView dequeueReusableCellWithIdentifier:@"JMImageTableViewCell"];
if(cell == nil) cell = [[DemoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"JMImageTableViewCell"];
static NSString *CellIdentifier = @"Cell";

NSString *urlString = [[self.modelArray objectAtIndex:indexPath.row] objectForKey:@"ImageURL"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

[cell.imageView setImageWithURL:[NSURL URLWithString:urlString]
placeholder:[UIImage imageNamed:@"placeholder"]];
NSString *urlString = [[self.modelArray objectAtIndex:indexPath.row] objectForKey:@"ImageURL"];

cell.textLabel.text = [[self.modelArray objectAtIndex:indexPath.row] objectForKey:@"Title"];

[cell.imageView setImageWithURL:[NSURL URLWithString:urlString]
placeholder:[UIImage imageNamed:@"placeholder"]];

return cell;
}

Expand Down
18 changes: 12 additions & 6 deletions Demo/JMImageCacheDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; };
288765A50DF7441C002DB57D /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 288765A40DF7441C002DB57D /* CoreGraphics.framework */; };
30B341EB166D1F9100B8F678 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 30B341E8166D1F9100B8F678 /* [email protected] */; };
30B341EC166D1F9100B8F678 /* Default.png in Resources */ = {isa = PBXBuildFile; fileRef = 30B341E9166D1F9100B8F678 /* Default.png */; };
30B341ED166D1F9100B8F678 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 30B341EA166D1F9100B8F678 /* [email protected] */; };
30BD1D5015BE205700792C47 /* UIImageView+JMImageCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 30BD1D4F15BE205700792C47 /* UIImageView+JMImageCache.m */; };
30BD1D5415BE2BF000792C47 /* placeholder.png in Resources */ = {isa = PBXBuildFile; fileRef = 30BD1D5215BE2BF000792C47 /* placeholder.png */; };
30BD1D5515BE2BF000792C47 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 30BD1D5315BE2BF000792C47 /* [email protected] */; };
B2C2B02A131072D100BCA7EB /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C2B020131072D100BCA7EB /* AppDelegate.m */; };
B2C2B02B131072D100BCA7EB /* DemoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C2B022131072D100BCA7EB /* DemoViewController.m */; };
B2C2B02C131072D100BCA7EB /* DemoTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C2B024131072D100BCA7EB /* DemoTableViewCell.m */; };
B2C2B02D131072D100BCA7EB /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C2B027131072D100BCA7EB /* main.m */; };
B2C2B033131072E200BCA7EB /* JMImageCache.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C2B032131072E200BCA7EB /* JMImageCache.m */; };
/* End PBXBuildFile section */
Expand All @@ -25,6 +27,9 @@
1D6058910D05DD3D006BFB54 /* JMImageCacheDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JMImageCacheDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
1DF5F4DF0D08C38300B7A737 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
288765A40DF7441C002DB57D /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
30B341E8166D1F9100B8F678 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
30B341E9166D1F9100B8F678 /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Default.png; sourceTree = "<group>"; };
30B341EA166D1F9100B8F678 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
30BD1D4E15BE205700792C47 /* UIImageView+JMImageCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImageView+JMImageCache.h"; path = "../UIImageView+JMImageCache.h"; sourceTree = "<group>"; };
30BD1D4F15BE205700792C47 /* UIImageView+JMImageCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImageView+JMImageCache.m"; path = "../UIImageView+JMImageCache.m"; sourceTree = "<group>"; };
30BD1D5215BE2BF000792C47 /* placeholder.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = placeholder.png; sourceTree = "<group>"; };
Expand All @@ -33,8 +38,6 @@
B2C2B020131072D100BCA7EB /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
B2C2B021131072D100BCA7EB /* DemoViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoViewController.h; sourceTree = "<group>"; };
B2C2B022131072D100BCA7EB /* DemoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoViewController.m; sourceTree = "<group>"; };
B2C2B023131072D100BCA7EB /* DemoTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoTableViewCell.h; sourceTree = "<group>"; };
B2C2B024131072D100BCA7EB /* DemoTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoTableViewCell.m; sourceTree = "<group>"; };
B2C2B026131072D100BCA7EB /* JMImageCacheDemo_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JMImageCacheDemo_Prefix.pch; sourceTree = "<group>"; };
B2C2B027131072D100BCA7EB /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
B2C2B029131072D100BCA7EB /* JMImageCacheDemo-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "JMImageCacheDemo-Info.plist"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -90,6 +93,9 @@
30BD1D5115BE2BF000792C47 /* Images */ = {
isa = PBXGroup;
children = (
30B341E8166D1F9100B8F678 /* [email protected] */,
30B341E9166D1F9100B8F678 /* Default.png */,
30B341EA166D1F9100B8F678 /* [email protected] */,
30BD1D5215BE2BF000792C47 /* placeholder.png */,
30BD1D5315BE2BF000792C47 /* [email protected] */,
);
Expand All @@ -103,8 +109,6 @@
B2C2B020131072D100BCA7EB /* AppDelegate.m */,
B2C2B021131072D100BCA7EB /* DemoViewController.h */,
B2C2B022131072D100BCA7EB /* DemoViewController.m */,
B2C2B023131072D100BCA7EB /* DemoTableViewCell.h */,
B2C2B024131072D100BCA7EB /* DemoTableViewCell.m */,
);
path = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -192,6 +196,9 @@
files = (
30BD1D5415BE2BF000792C47 /* placeholder.png in Resources */,
30BD1D5515BE2BF000792C47 /* [email protected] in Resources */,
30B341EB166D1F9100B8F678 /* [email protected] in Resources */,
30B341EC166D1F9100B8F678 /* Default.png in Resources */,
30B341ED166D1F9100B8F678 /* [email protected] in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -204,7 +211,6 @@
files = (
B2C2B02A131072D100BCA7EB /* AppDelegate.m in Sources */,
B2C2B02B131072D100BCA7EB /* DemoViewController.m in Sources */,
B2C2B02C131072D100BCA7EB /* DemoTableViewCell.m in Sources */,
B2C2B02D131072D100BCA7EB /* main.m in Sources */,
B2C2B033131072E200BCA7EB /* JMImageCache.m in Sources */,
30BD1D5015BE205700792C47 /* UIImageView+JMImageCache.m in Sources */,
Expand Down
Binary file added Demo/Resources/Images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Demo/Resources/Images/Default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Demo/Resources/Images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 24 additions & 8 deletions UIImageView+JMImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,39 @@ - (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage {
}
- (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage {
self.jm_imageURL = url;
self.image = placeholderImage;

[self setNeedsDisplay];
[self setNeedsLayout];

__weak UIImageView *safeSelf = self;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *i;

if (key) {
i = [[JMImageCache sharedCache] cachedImageForKey:key];
} else {
i = [[JMImageCache sharedCache] cachedImageForURL:url];
}

if(i) {
dispatch_async(dispatch_get_main_queue(), ^{
self.image = i;
self.jm_imageURL = nil;
safeSelf.jm_imageURL = nil;

safeSelf.image = i;

[safeSelf setNeedsLayout];
[safeSelf setNeedsDisplay];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
self.image = placeholderImage;
safeSelf.image = placeholderImage;

[safeSelf setNeedsDisplay];
[safeSelf setNeedsLayout];
});

__weak UIImageView *safeSelf = self;


[[JMImageCache sharedCache] imageForURL:url key:key completionBlock:^(UIImage *image) {
if ([url isEqual:safeSelf.jm_imageURL]) {
dispatch_async(dispatch_get_main_queue(), ^{
Expand All @@ -74,7 +86,11 @@ - (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)
} else {
safeSelf.image = placeholderImage;
}

safeSelf.jm_imageURL = nil;

[safeSelf setNeedsLayout];
[safeSelf setNeedsDisplay];
});
}
}];
Expand Down

0 comments on commit 229b10d

Please sign in to comment.