├── .gitignore ├── Demo ├── Classes │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── DemoViewController.h │ └── DemoViewController.m ├── JMImageCacheDemo.xcodeproj │ └── project.pbxproj ├── Other Sources │ ├── JMImageCacheDemo_Prefix.pch │ └── main.m └── Resources │ ├── Images │ ├── Default-568h@2x.png │ ├── Default.png │ ├── Default@2x.png │ ├── placeholder.png │ └── placeholder@2x.png │ └── JMImageCacheDemo-Info.plist ├── JMImageCache.h ├── JMImageCache.m ├── JMImageCache.podspec ├── MIT-LICENSE ├── README.markdown ├── UIImageView+JMImageCache.h └── UIImageView+JMImageCache.m /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | build 3 | build/* 4 | 5 | *.mode1v3 6 | *.pbxuser 7 | *.perspectivev3 8 | *.tm_build_errors 9 | *.mode1v3 10 | *.pbxuser 11 | xcuserdata 12 | project.xcworkspace 13 | -------------------------------------------------------------------------------- /Demo/Classes/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // JMCacheAppDelegate.h 3 | // JMCache 4 | // 5 | // Created by Jake Marsh on 2/7/11. 6 | // Copyright 2011 Jake Marsh. All rights reserved. 7 | // 8 | 9 | @interface AppDelegate : NSObject 10 | 11 | 12 | @end 13 | -------------------------------------------------------------------------------- /Demo/Classes/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // JMCacheAppDelegate.m 3 | // JMCache 4 | // 5 | // Created by Jake Marsh on 2/7/11. 6 | // Copyright 2011 Jake Marsh. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "DemoViewController.h" 11 | 12 | @implementation AppDelegate 13 | 14 | @synthesize window = _window; 15 | 16 | #pragma mark - 17 | #pragma mark Application lifecycle 18 | 19 | - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 20 | self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 21 | 22 | DemoViewController *viewController = [[DemoViewController alloc] init]; 23 | UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 24 | 25 | self.window.rootViewController = mainNavigationController; 26 | 27 | [self.window makeKeyAndVisible]; 28 | 29 | return YES; 30 | } 31 | 32 | @end -------------------------------------------------------------------------------- /Demo/Classes/DemoViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // JMCacheViewController.h 3 | // JMCache 4 | // 5 | // Created by Jake Marsh on 2/7/11. 6 | // Copyright 2011 Jake Marsh. All rights reserved. 7 | // 8 | 9 | @interface DemoViewController : UITableViewController 10 | 11 | @end 12 | -------------------------------------------------------------------------------- /Demo/Classes/DemoViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // JMCacheViewController.m 3 | // JMCache 4 | // 5 | // Created by Jake Marsh on 2/7/11. 6 | // Copyright 2011 Jake Marsh. All rights reserved. 7 | // 8 | 9 | #import "DemoViewController.h" 10 | #import "JMImageCache.h" 11 | 12 | @interface DemoViewController () 13 | 14 | @property (strong, nonatomic) NSMutableArray *modelArray; 15 | 16 | @end 17 | 18 | @implementation DemoViewController 19 | 20 | @synthesize modelArray = _modelArray; 21 | 22 | - (id) init { 23 | self = [super init]; 24 | if(!self) return nil; 25 | 26 | self.title = @"The Office"; 27 | 28 | self.modelArray = [[NSMutableArray alloc] init]; 29 | 30 | [self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4hCR/Untitled-7.png", @"ImageURL", @"Michael Scott", @"Title", nil]]; 31 | [self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4iIc/Untitled-7.png", @"ImageURL", @"Jim Halpert", @"Title", nil]]; 32 | [self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4hVv/Untitled-7.png", @"ImageURL", @"Pam Beasley-Halpert", @"Title", nil]]; 33 | [self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4hF3/Untitled-7.png", @"ImageURL", @"Dwight Schrute", @"Title", nil]]; 34 | [self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4hxj/Untitled-7.png", @"ImageURL", @"Andy Bernard", @"Title", nil]]; 35 | [self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4iNI/Untitled-7.png", @"ImageURL", @"Kevin Malone", @"Title", nil]]; 36 | [self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4iAX/Untitled-7.png", @"ImageURL", @"Stanley Hudson", @"Title", nil]]; 37 | 38 | // You should remove this next line from your apps!!! 39 | // 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. 40 | 41 | [[JMImageCache sharedCache] removeAllObjects]; 42 | 43 | return self; 44 | } 45 | 46 | #pragma mark - 47 | #pragma mark Autorotation Methods 48 | 49 | - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 50 | return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown; 51 | } 52 | 53 | #pragma mark - 54 | #pragma mark Cleanup Methods 55 | 56 | - (void) didReceiveMemoryWarning { 57 | [super didReceiveMemoryWarning]; 58 | } 59 | 60 | #pragma mark - 61 | #pragma mark UITableViewDataSource 62 | 63 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 64 | return [self.modelArray count]; 65 | } 66 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 67 | static NSString *CellIdentifier = @"Cell"; 68 | 69 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 70 | if(cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 71 | 72 | NSString *urlString = [[self.modelArray objectAtIndex:indexPath.row] objectForKey:@"ImageURL"]; 73 | 74 | cell.textLabel.text = [[self.modelArray objectAtIndex:indexPath.row] objectForKey:@"Title"]; 75 | 76 | [cell.imageView setImageWithURL:[NSURL URLWithString:urlString] 77 | placeholder:[UIImage imageNamed:@"placeholder"]]; 78 | 79 | return cell; 80 | } 81 | 82 | #pragma mark - 83 | #pragma mark UITableViewDelegate 84 | 85 | - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 86 | return 90.0; 87 | } 88 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 89 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; 90 | } 91 | 92 | @end -------------------------------------------------------------------------------- /Demo/JMImageCacheDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; }; 11 | 1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; }; 12 | 288765A50DF7441C002DB57D /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 288765A40DF7441C002DB57D /* CoreGraphics.framework */; }; 13 | 30B341EB166D1F9100B8F678 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 30B341E8166D1F9100B8F678 /* Default-568h@2x.png */; }; 14 | 30B341EC166D1F9100B8F678 /* Default.png in Resources */ = {isa = PBXBuildFile; fileRef = 30B341E9166D1F9100B8F678 /* Default.png */; }; 15 | 30B341ED166D1F9100B8F678 /* Default@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 30B341EA166D1F9100B8F678 /* Default@2x.png */; }; 16 | 30BD1D5015BE205700792C47 /* UIImageView+JMImageCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 30BD1D4F15BE205700792C47 /* UIImageView+JMImageCache.m */; }; 17 | 30BD1D5415BE2BF000792C47 /* placeholder.png in Resources */ = {isa = PBXBuildFile; fileRef = 30BD1D5215BE2BF000792C47 /* placeholder.png */; }; 18 | 30BD1D5515BE2BF000792C47 /* placeholder@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 30BD1D5315BE2BF000792C47 /* placeholder@2x.png */; }; 19 | B2C2B02A131072D100BCA7EB /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C2B020131072D100BCA7EB /* AppDelegate.m */; }; 20 | B2C2B02B131072D100BCA7EB /* DemoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C2B022131072D100BCA7EB /* DemoViewController.m */; }; 21 | B2C2B02D131072D100BCA7EB /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C2B027131072D100BCA7EB /* main.m */; }; 22 | B2C2B033131072E200BCA7EB /* JMImageCache.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C2B032131072E200BCA7EB /* JMImageCache.m */; }; 23 | /* End PBXBuildFile section */ 24 | 25 | /* Begin PBXFileReference section */ 26 | 1D30AB110D05D00D00671497 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 27 | 1D6058910D05DD3D006BFB54 /* JMImageCacheDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JMImageCacheDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 28 | 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 29 | 288765A40DF7441C002DB57D /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; 30 | 30B341E8166D1F9100B8F678 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; 31 | 30B341E9166D1F9100B8F678 /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Default.png; sourceTree = ""; }; 32 | 30B341EA166D1F9100B8F678 /* Default@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default@2x.png"; sourceTree = ""; }; 33 | 30BD1D4E15BE205700792C47 /* UIImageView+JMImageCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImageView+JMImageCache.h"; path = "../UIImageView+JMImageCache.h"; sourceTree = ""; }; 34 | 30BD1D4F15BE205700792C47 /* UIImageView+JMImageCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImageView+JMImageCache.m"; path = "../UIImageView+JMImageCache.m"; sourceTree = ""; }; 35 | 30BD1D5215BE2BF000792C47 /* placeholder.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = placeholder.png; sourceTree = ""; }; 36 | 30BD1D5315BE2BF000792C47 /* placeholder@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "placeholder@2x.png"; sourceTree = ""; }; 37 | B2C2B01F131072D100BCA7EB /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 38 | B2C2B020131072D100BCA7EB /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 39 | B2C2B021131072D100BCA7EB /* DemoViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoViewController.h; sourceTree = ""; }; 40 | B2C2B022131072D100BCA7EB /* DemoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoViewController.m; sourceTree = ""; }; 41 | B2C2B026131072D100BCA7EB /* JMImageCacheDemo_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JMImageCacheDemo_Prefix.pch; sourceTree = ""; }; 42 | B2C2B027131072D100BCA7EB /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 43 | B2C2B029131072D100BCA7EB /* JMImageCacheDemo-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "JMImageCacheDemo-Info.plist"; sourceTree = ""; }; 44 | B2C2B031131072E200BCA7EB /* JMImageCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JMImageCache.h; path = ../JMImageCache.h; sourceTree = ""; }; 45 | B2C2B032131072E200BCA7EB /* JMImageCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = JMImageCache.m; path = ../JMImageCache.m; sourceTree = ""; }; 46 | /* End PBXFileReference section */ 47 | 48 | /* Begin PBXFrameworksBuildPhase section */ 49 | 1D60588F0D05DD3D006BFB54 /* Frameworks */ = { 50 | isa = PBXFrameworksBuildPhase; 51 | buildActionMask = 2147483647; 52 | files = ( 53 | 1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */, 54 | 1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */, 55 | 288765A50DF7441C002DB57D /* CoreGraphics.framework in Frameworks */, 56 | ); 57 | runOnlyForDeploymentPostprocessing = 0; 58 | }; 59 | /* End PBXFrameworksBuildPhase section */ 60 | 61 | /* Begin PBXGroup section */ 62 | 19C28FACFE9D520D11CA2CBB /* Products */ = { 63 | isa = PBXGroup; 64 | children = ( 65 | 1D6058910D05DD3D006BFB54 /* JMImageCacheDemo.app */, 66 | ); 67 | name = Products; 68 | sourceTree = ""; 69 | }; 70 | 29B97314FDCFA39411CA2CEA /* CustomTemplate */ = { 71 | isa = PBXGroup; 72 | children = ( 73 | B2C2B030131072D800BCA7EB /* JMImageCache */, 74 | B2C2B01E131072D100BCA7EB /* Classes */, 75 | B2C2B025131072D100BCA7EB /* Other Sources */, 76 | B2C2B028131072D100BCA7EB /* Resources */, 77 | 29B97323FDCFA39411CA2CEA /* Frameworks */, 78 | 19C28FACFE9D520D11CA2CBB /* Products */, 79 | ); 80 | name = CustomTemplate; 81 | sourceTree = ""; 82 | }; 83 | 29B97323FDCFA39411CA2CEA /* Frameworks */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */, 87 | 1D30AB110D05D00D00671497 /* Foundation.framework */, 88 | 288765A40DF7441C002DB57D /* CoreGraphics.framework */, 89 | ); 90 | name = Frameworks; 91 | sourceTree = ""; 92 | }; 93 | 30BD1D5115BE2BF000792C47 /* Images */ = { 94 | isa = PBXGroup; 95 | children = ( 96 | 30B341E8166D1F9100B8F678 /* Default-568h@2x.png */, 97 | 30B341E9166D1F9100B8F678 /* Default.png */, 98 | 30B341EA166D1F9100B8F678 /* Default@2x.png */, 99 | 30BD1D5215BE2BF000792C47 /* placeholder.png */, 100 | 30BD1D5315BE2BF000792C47 /* placeholder@2x.png */, 101 | ); 102 | path = Images; 103 | sourceTree = ""; 104 | }; 105 | B2C2B01E131072D100BCA7EB /* Classes */ = { 106 | isa = PBXGroup; 107 | children = ( 108 | B2C2B01F131072D100BCA7EB /* AppDelegate.h */, 109 | B2C2B020131072D100BCA7EB /* AppDelegate.m */, 110 | B2C2B021131072D100BCA7EB /* DemoViewController.h */, 111 | B2C2B022131072D100BCA7EB /* DemoViewController.m */, 112 | ); 113 | path = Classes; 114 | sourceTree = ""; 115 | }; 116 | B2C2B025131072D100BCA7EB /* Other Sources */ = { 117 | isa = PBXGroup; 118 | children = ( 119 | B2C2B026131072D100BCA7EB /* JMImageCacheDemo_Prefix.pch */, 120 | B2C2B027131072D100BCA7EB /* main.m */, 121 | ); 122 | path = "Other Sources"; 123 | sourceTree = ""; 124 | }; 125 | B2C2B028131072D100BCA7EB /* Resources */ = { 126 | isa = PBXGroup; 127 | children = ( 128 | 30BD1D5115BE2BF000792C47 /* Images */, 129 | B2C2B029131072D100BCA7EB /* JMImageCacheDemo-Info.plist */, 130 | ); 131 | path = Resources; 132 | sourceTree = ""; 133 | }; 134 | B2C2B030131072D800BCA7EB /* JMImageCache */ = { 135 | isa = PBXGroup; 136 | children = ( 137 | B2C2B031131072E200BCA7EB /* JMImageCache.h */, 138 | B2C2B032131072E200BCA7EB /* JMImageCache.m */, 139 | 30BD1D4E15BE205700792C47 /* UIImageView+JMImageCache.h */, 140 | 30BD1D4F15BE205700792C47 /* UIImageView+JMImageCache.m */, 141 | ); 142 | name = JMImageCache; 143 | sourceTree = ""; 144 | }; 145 | /* End PBXGroup section */ 146 | 147 | /* Begin PBXNativeTarget section */ 148 | 1D6058900D05DD3D006BFB54 /* JMImageCacheDemo */ = { 149 | isa = PBXNativeTarget; 150 | buildConfigurationList = 1D6058960D05DD3E006BFB54 /* Build configuration list for PBXNativeTarget "JMImageCacheDemo" */; 151 | buildPhases = ( 152 | 1D60588D0D05DD3D006BFB54 /* Resources */, 153 | 1D60588E0D05DD3D006BFB54 /* Sources */, 154 | 1D60588F0D05DD3D006BFB54 /* Frameworks */, 155 | ); 156 | buildRules = ( 157 | ); 158 | dependencies = ( 159 | ); 160 | name = JMImageCacheDemo; 161 | productName = JMCache; 162 | productReference = 1D6058910D05DD3D006BFB54 /* JMImageCacheDemo.app */; 163 | productType = "com.apple.product-type.application"; 164 | }; 165 | /* End PBXNativeTarget section */ 166 | 167 | /* Begin PBXProject section */ 168 | 29B97313FDCFA39411CA2CEA /* Project object */ = { 169 | isa = PBXProject; 170 | attributes = { 171 | LastUpgradeCheck = 0440; 172 | }; 173 | buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "JMImageCacheDemo" */; 174 | compatibilityVersion = "Xcode 3.2"; 175 | developmentRegion = English; 176 | hasScannedForEncodings = 1; 177 | knownRegions = ( 178 | English, 179 | Japanese, 180 | French, 181 | German, 182 | ); 183 | mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */; 184 | projectDirPath = ""; 185 | projectRoot = ""; 186 | targets = ( 187 | 1D6058900D05DD3D006BFB54 /* JMImageCacheDemo */, 188 | ); 189 | }; 190 | /* End PBXProject section */ 191 | 192 | /* Begin PBXResourcesBuildPhase section */ 193 | 1D60588D0D05DD3D006BFB54 /* Resources */ = { 194 | isa = PBXResourcesBuildPhase; 195 | buildActionMask = 2147483647; 196 | files = ( 197 | 30BD1D5415BE2BF000792C47 /* placeholder.png in Resources */, 198 | 30BD1D5515BE2BF000792C47 /* placeholder@2x.png in Resources */, 199 | 30B341EB166D1F9100B8F678 /* Default-568h@2x.png in Resources */, 200 | 30B341EC166D1F9100B8F678 /* Default.png in Resources */, 201 | 30B341ED166D1F9100B8F678 /* Default@2x.png in Resources */, 202 | ); 203 | runOnlyForDeploymentPostprocessing = 0; 204 | }; 205 | /* End PBXResourcesBuildPhase section */ 206 | 207 | /* Begin PBXSourcesBuildPhase section */ 208 | 1D60588E0D05DD3D006BFB54 /* Sources */ = { 209 | isa = PBXSourcesBuildPhase; 210 | buildActionMask = 2147483647; 211 | files = ( 212 | B2C2B02A131072D100BCA7EB /* AppDelegate.m in Sources */, 213 | B2C2B02B131072D100BCA7EB /* DemoViewController.m in Sources */, 214 | B2C2B02D131072D100BCA7EB /* main.m in Sources */, 215 | B2C2B033131072E200BCA7EB /* JMImageCache.m in Sources */, 216 | 30BD1D5015BE205700792C47 /* UIImageView+JMImageCache.m in Sources */, 217 | ); 218 | runOnlyForDeploymentPostprocessing = 0; 219 | }; 220 | /* End PBXSourcesBuildPhase section */ 221 | 222 | /* Begin XCBuildConfiguration section */ 223 | 1D6058940D05DD3E006BFB54 /* Debug */ = { 224 | isa = XCBuildConfiguration; 225 | buildSettings = { 226 | ALWAYS_SEARCH_USER_PATHS = NO; 227 | CLANG_ENABLE_OBJC_ARC = YES; 228 | COPY_PHASE_STRIP = NO; 229 | GCC_DYNAMIC_NO_PIC = NO; 230 | GCC_OPTIMIZATION_LEVEL = 0; 231 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 232 | GCC_PREFIX_HEADER = "Other Sources/JMImageCacheDemo_Prefix.pch"; 233 | GCC_VERSION = com.apple.compilers.llvm.clang.1_0; 234 | INFOPLIST_FILE = "Resources/JMImageCacheDemo-Info.plist"; 235 | PRODUCT_NAME = JMImageCacheDemo; 236 | }; 237 | name = Debug; 238 | }; 239 | 1D6058950D05DD3E006BFB54 /* Release */ = { 240 | isa = XCBuildConfiguration; 241 | buildSettings = { 242 | ALWAYS_SEARCH_USER_PATHS = NO; 243 | CLANG_ENABLE_OBJC_ARC = YES; 244 | COPY_PHASE_STRIP = YES; 245 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 246 | GCC_PREFIX_HEADER = "Other Sources/JMImageCacheDemo_Prefix.pch"; 247 | GCC_VERSION = com.apple.compilers.llvm.clang.1_0; 248 | INFOPLIST_FILE = "Resources/JMImageCacheDemo-Info.plist"; 249 | PRODUCT_NAME = JMImageCacheDemo; 250 | VALIDATE_PRODUCT = YES; 251 | }; 252 | name = Release; 253 | }; 254 | C01FCF4F08A954540054247B /* Debug */ = { 255 | isa = XCBuildConfiguration; 256 | buildSettings = { 257 | ARCHS = "$(ARCHS_STANDARD_32_BIT)"; 258 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 259 | GCC_C_LANGUAGE_STANDARD = c99; 260 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 261 | GCC_WARN_UNUSED_VARIABLE = YES; 262 | SDKROOT = iphoneos; 263 | }; 264 | name = Debug; 265 | }; 266 | C01FCF5008A954540054247B /* Release */ = { 267 | isa = XCBuildConfiguration; 268 | buildSettings = { 269 | ARCHS = "$(ARCHS_STANDARD_32_BIT)"; 270 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 271 | GCC_C_LANGUAGE_STANDARD = c99; 272 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 273 | GCC_WARN_UNUSED_VARIABLE = YES; 274 | OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1"; 275 | SDKROOT = iphoneos; 276 | }; 277 | name = Release; 278 | }; 279 | /* End XCBuildConfiguration section */ 280 | 281 | /* Begin XCConfigurationList section */ 282 | 1D6058960D05DD3E006BFB54 /* Build configuration list for PBXNativeTarget "JMImageCacheDemo" */ = { 283 | isa = XCConfigurationList; 284 | buildConfigurations = ( 285 | 1D6058940D05DD3E006BFB54 /* Debug */, 286 | 1D6058950D05DD3E006BFB54 /* Release */, 287 | ); 288 | defaultConfigurationIsVisible = 0; 289 | defaultConfigurationName = Release; 290 | }; 291 | C01FCF4E08A954540054247B /* Build configuration list for PBXProject "JMImageCacheDemo" */ = { 292 | isa = XCConfigurationList; 293 | buildConfigurations = ( 294 | C01FCF4F08A954540054247B /* Debug */, 295 | C01FCF5008A954540054247B /* Release */, 296 | ); 297 | defaultConfigurationIsVisible = 0; 298 | defaultConfigurationName = Release; 299 | }; 300 | /* End XCConfigurationList section */ 301 | }; 302 | rootObject = 29B97313FDCFA39411CA2CEA /* Project object */; 303 | } 304 | -------------------------------------------------------------------------------- /Demo/Other Sources/JMImageCacheDemo_Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header for all source files of the 'JMCache' target in the 'JMCache' project 3 | // 4 | 5 | #ifdef __OBJC__ 6 | #import 7 | #import 8 | #endif -------------------------------------------------------------------------------- /Demo/Other Sources/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // JMCache 4 | // 5 | // Created by Jake Marsh on 2/7/11. 6 | // Copyright 2011 Rubber Duck Software. All rights reserved. 7 | // 8 | 9 | int main(int argc, char *argv[]) { 10 | 11 | @autoreleasepool { 12 | int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate"); 13 | return retVal; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Demo/Resources/Images/Default-568h@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakemarsh/JMImageCache/cd355a4c395ba5f380ddaafd11bc6a513402f898/Demo/Resources/Images/Default-568h@2x.png -------------------------------------------------------------------------------- /Demo/Resources/Images/Default.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakemarsh/JMImageCache/cd355a4c395ba5f380ddaafd11bc6a513402f898/Demo/Resources/Images/Default.png -------------------------------------------------------------------------------- /Demo/Resources/Images/Default@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakemarsh/JMImageCache/cd355a4c395ba5f380ddaafd11bc6a513402f898/Demo/Resources/Images/Default@2x.png -------------------------------------------------------------------------------- /Demo/Resources/Images/placeholder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakemarsh/JMImageCache/cd355a4c395ba5f380ddaafd11bc6a513402f898/Demo/Resources/Images/placeholder.png -------------------------------------------------------------------------------- /Demo/Resources/Images/placeholder@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakemarsh/JMImageCache/cd355a4c395ba5f380ddaafd11bc6a513402f898/Demo/Resources/Images/placeholder@2x.png -------------------------------------------------------------------------------- /Demo/Resources/JMImageCacheDemo-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIconFile 12 | 13 | CFBundleIdentifier 14 | com.jakemarsh.${PRODUCT_NAME:rfc1034identifier} 15 | CFBundleInfoDictionaryVersion 16 | 6.0 17 | CFBundleName 18 | ${PRODUCT_NAME} 19 | CFBundlePackageType 20 | APPL 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /JMImageCache.h: -------------------------------------------------------------------------------- 1 | // 2 | // JMImageCache.h 3 | // JMCache 4 | // 5 | // Created by Jake Marsh on 2/7/11. 6 | // Copyright 2011 Jake Marsh. All rights reserved. 7 | // 8 | 9 | #import "UIImageView+JMImageCache.h" 10 | 11 | @class JMImageCache; 12 | 13 | @protocol JMImageCacheDelegate 14 | 15 | @optional 16 | - (void) cache:(JMImageCache *)c didDownloadImage:(UIImage *)i forURL:(NSURL *)url; 17 | - (void) cache:(JMImageCache *)c didDownloadImage:(UIImage *)i forURL:(NSURL *)url key:(NSString*)key; 18 | 19 | @end 20 | 21 | @interface JMImageCache : NSCache 22 | 23 | + (JMImageCache *) sharedCache; 24 | 25 | - (void) imageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure; 26 | - (void) imageForURL:(NSURL *)url completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure; 27 | 28 | - (UIImage *) cachedImageForKey:(NSString *)key; 29 | - (UIImage *) cachedImageForURL:(NSURL *)url; 30 | 31 | - (UIImage *) imageForURL:(NSURL *)url key:(NSString*)key delegate:(id)d; 32 | - (UIImage *) imageForURL:(NSURL *)url delegate:(id)d; 33 | 34 | - (UIImage *) imageFromDiskForKey:(NSString *)key; 35 | - (UIImage *) imageFromDiskForURL:(NSURL *)url; 36 | 37 | - (void) setImage:(UIImage *)i forKey:(NSString *)key; 38 | - (void) setImage:(UIImage *)i forURL:(NSURL *)url; 39 | - (void) removeImageForKey:(NSString *)key; 40 | - (void) removeImageForURL:(NSURL *)url; 41 | 42 | - (void) writeData:(NSData *)data toPath:(NSString *)path; 43 | - (void) performDiskWriteOperation:(NSInvocation *)invoction; 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /JMImageCache.m: -------------------------------------------------------------------------------- 1 | // 2 | // JMImageCache.m 3 | // JMCache 4 | // 5 | // Created by Jake Marsh on 2/7/11. 6 | // Copyright 2011 Jake Marsh. All rights reserved. 7 | // 8 | 9 | #import "JMImageCache.h" 10 | 11 | static inline NSString *JMImageCacheDirectory() { 12 | static NSString *_JMImageCacheDirectory; 13 | static dispatch_once_t onceToken; 14 | 15 | dispatch_once(&onceToken, ^{ 16 | _JMImageCacheDirectory = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/JMCache"] copy]; 17 | }); 18 | 19 | return _JMImageCacheDirectory; 20 | } 21 | inline static NSString *keyForURL(NSURL *url) { 22 | return [url absoluteString]; 23 | } 24 | static inline NSString *cachePathForKey(NSString *key) { 25 | NSString *fileName = [NSString stringWithFormat:@"JMImageCache-%@", [JMImageCache SHA1FromString:key]]; 26 | return [JMImageCacheDirectory() stringByAppendingPathComponent:fileName]; 27 | } 28 | 29 | @interface JMImageCache () 30 | 31 | @property (strong, nonatomic) NSOperationQueue *diskOperationQueue; 32 | 33 | - (void) _downloadAndWriteImageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure; 34 | 35 | @end 36 | 37 | @implementation JMImageCache 38 | 39 | @synthesize diskOperationQueue = _diskOperationQueue; 40 | 41 | + (JMImageCache *) sharedCache { 42 | static JMImageCache *_sharedCache = nil; 43 | static dispatch_once_t onceToken; 44 | 45 | dispatch_once(&onceToken, ^{ 46 | _sharedCache = [[JMImageCache alloc] init]; 47 | }); 48 | 49 | return _sharedCache; 50 | } 51 | 52 | - (id) init { 53 | self = [super init]; 54 | if(!self) return nil; 55 | 56 | self.diskOperationQueue = [[NSOperationQueue alloc] init]; 57 | 58 | [[NSFileManager defaultManager] createDirectoryAtPath:JMImageCacheDirectory() 59 | withIntermediateDirectories:YES 60 | attributes:nil 61 | error:NULL]; 62 | return self; 63 | } 64 | 65 | - (void) _downloadAndWriteImageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure 66 | { 67 | if (!key && !url) return; 68 | 69 | if (!key) { 70 | key = keyForURL(url); 71 | } 72 | 73 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 74 | 75 | NSURLRequest* request = [NSURLRequest requestWithURL:url]; 76 | NSURLResponse* response = nil; 77 | NSError* error = nil; 78 | NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 79 | 80 | if (error) 81 | { 82 | dispatch_async(dispatch_get_main_queue(), ^{ 83 | 84 | if(failure) failure(request, response, error); 85 | }); 86 | return; 87 | } 88 | 89 | UIImage *i = [[UIImage alloc] initWithData:data]; 90 | if (!i) 91 | { 92 | NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary]; 93 | [errorDetail setValue:[NSString stringWithFormat:@"Failed to init image with data from for URL: %@", url] forKey:NSLocalizedDescriptionKey]; 94 | NSError* error = [NSError errorWithDomain:@"JMImageCacheErrorDomain" code:1 userInfo:errorDetail]; 95 | dispatch_async(dispatch_get_main_queue(), ^{ 96 | 97 | if(failure) failure(request, response, error); 98 | }); 99 | } 100 | else 101 | { 102 | NSString *cachePath = cachePathForKey(key); 103 | NSInvocation *writeInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(writeData:toPath:)]]; 104 | 105 | [writeInvocation setTarget:self]; 106 | [writeInvocation setSelector:@selector(writeData:toPath:)]; 107 | [writeInvocation setArgument:&data atIndex:2]; 108 | [writeInvocation setArgument:&cachePath atIndex:3]; 109 | 110 | [self performDiskWriteOperation:writeInvocation]; 111 | [self setImage:i forKey:key]; 112 | 113 | dispatch_async(dispatch_get_main_queue(), ^{ 114 | if(completion) completion(i); 115 | }); 116 | } 117 | }); 118 | } 119 | 120 | - (void) removeAllObjects { 121 | [super removeAllObjects]; 122 | 123 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 124 | NSFileManager *fileMgr = [NSFileManager defaultManager]; 125 | NSError *error = nil; 126 | NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:JMImageCacheDirectory() error:&error]; 127 | 128 | if (error == nil) { 129 | for (NSString *path in directoryContents) { 130 | NSString *fullPath = [JMImageCacheDirectory() stringByAppendingPathComponent:path]; 131 | 132 | BOOL removeSuccess = [fileMgr removeItemAtPath:fullPath error:&error]; 133 | if (!removeSuccess) { 134 | //Error Occured 135 | } 136 | } 137 | } else { 138 | //Error Occured 139 | } 140 | }); 141 | } 142 | - (void) removeObjectForKey:(id)key { 143 | [super removeObjectForKey:key]; 144 | 145 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 146 | NSFileManager *fileMgr = [NSFileManager defaultManager]; 147 | NSString *cachePath = cachePathForKey(key); 148 | 149 | NSError *error = nil; 150 | 151 | BOOL removeSuccess = [fileMgr removeItemAtPath:cachePath error:&error]; 152 | if (!removeSuccess) { 153 | //Error Occured 154 | } 155 | }); 156 | } 157 | 158 | #pragma mark - 159 | #pragma mark Getter Methods 160 | 161 | - (void) imageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure{ 162 | 163 | UIImage *i = [self cachedImageForKey:key]; 164 | 165 | if(i) { 166 | if(completion) completion(i); 167 | } else { 168 | [self _downloadAndWriteImageForURL:url key:key completionBlock:completion failureBlock:failure]; 169 | } 170 | } 171 | 172 | - (void) imageForURL:(NSURL *)url completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure{ 173 | [self imageForURL:url key:keyForURL(url) completionBlock:completion failureBlock:(failure)]; 174 | } 175 | 176 | - (UIImage *) cachedImageForKey:(NSString *)key { 177 | if(!key) return nil; 178 | 179 | id returner = [super objectForKey:key]; 180 | 181 | if(returner) { 182 | return returner; 183 | } else { 184 | UIImage *i = [self imageFromDiskForKey:key]; 185 | if(i) [self setImage:i forKey:key]; 186 | 187 | return i; 188 | } 189 | 190 | return nil; 191 | } 192 | 193 | - (UIImage *) cachedImageForURL:(NSURL *)url { 194 | NSString *key = keyForURL(url); 195 | return [self cachedImageForKey:key]; 196 | } 197 | 198 | - (UIImage *) imageForURL:(NSURL *)url key:(NSString*)key delegate:(id)d { 199 | if(!url) return nil; 200 | 201 | UIImage *i = [self cachedImageForURL:url]; 202 | 203 | if(i) { 204 | return i; 205 | } else { 206 | [self _downloadAndWriteImageForURL:url key:key completionBlock:^(UIImage *image) { 207 | if(d) { 208 | if([d respondsToSelector:@selector(cache:didDownloadImage:forURL:)]) { 209 | [d cache:self didDownloadImage:image forURL:url]; 210 | } 211 | if([d respondsToSelector:@selector(cache:didDownloadImage:forURL:key:)]) { 212 | [d cache:self didDownloadImage:image forURL:url key:key]; 213 | } 214 | } 215 | } 216 | failureBlock:nil]; 217 | } 218 | 219 | return nil; 220 | } 221 | 222 | - (UIImage *) imageForURL:(NSURL *)url delegate:(id)d { 223 | return [self imageForURL:url key:keyForURL(url) delegate:d]; 224 | } 225 | 226 | - (UIImage *) imageFromDiskForKey:(NSString *)key { 227 | UIImage *i = [[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:cachePathForKey(key) options:0 error:NULL]]; 228 | return i; 229 | } 230 | 231 | - (UIImage *) imageFromDiskForURL:(NSURL *)url { 232 | return [self imageFromDiskForKey:keyForURL(url)]; 233 | } 234 | 235 | #pragma mark - 236 | #pragma mark Setter Methods 237 | 238 | - (void) setImage:(UIImage *)i forKey:(NSString *)key { 239 | if (i) { 240 | [super setObject:i forKey:key]; 241 | } 242 | } 243 | - (void) setImage:(UIImage *)i forURL:(NSURL *)url { 244 | [self setImage:i forKey:keyForURL(url)]; 245 | } 246 | - (void) removeImageForKey:(NSString *)key { 247 | [self removeObjectForKey:key]; 248 | } 249 | - (void) removeImageForURL:(NSURL *)url { 250 | [self removeImageForKey:keyForURL(url)]; 251 | } 252 | 253 | #pragma mark - 254 | #pragma mark Disk Writing Operations 255 | 256 | - (void) writeData:(NSData*)data toPath:(NSString *)path { 257 | [data writeToFile:path atomically:YES]; 258 | } 259 | - (void) performDiskWriteOperation:(NSInvocation *)invoction { 260 | NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invoction]; 261 | 262 | [self.diskOperationQueue addOperation:operation]; 263 | } 264 | 265 | #pragma mark - Hash methods 266 | 267 | + (NSString *)SHA1FromString:(NSString *)string 268 | { 269 | unsigned char digest[CC_SHA1_DIGEST_LENGTH]; 270 | 271 | NSData *stringBytes = [string dataUsingEncoding:NSUTF8StringEncoding]; 272 | 273 | if (CC_SHA1([stringBytes bytes], (CC_LONG)[stringBytes length], digest)) { 274 | 275 | NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2]; 276 | 277 | for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) { 278 | [output appendFormat:@"%02x", digest[i]]; 279 | } 280 | 281 | return output; 282 | } 283 | return nil; 284 | } 285 | 286 | @end -------------------------------------------------------------------------------- /JMImageCache.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "JMImageCache" 3 | s.version = "0.4.0" 4 | s.summary = "NSCache based remote-image caching and downloading mechanism for iOS." 5 | s.description = 'NSCache based remote-image caching and downloading mechanism for iOS. Is block based and uses a simple UIImageView category to handle loading images with placeholders. With fix to the placeholder behaviour' 6 | 7 | s.homepage = "https://github.com/jakemarsh/JMImageCache" 8 | 9 | s.license = { 10 | :type => 'MIT', 11 | :file => 'MIT-LICENSE' 12 | } 13 | 14 | s.authors = { "Jake Marsh" => "jake@deallocatedobjects.com" } 15 | 16 | s.source = { :git => "https://github.com/jakemarsh/JMImageCache.git", :tag => "0.4.0" } 17 | 18 | s.platform = :ios, '5.0' 19 | s.requires_arc = true 20 | s.source_files = ['*.h', '*.m'] 21 | end 22 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Jake Marsh and other contributors. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # JMImageCache 2 | 3 | ## Introduction 4 | 5 | `JMImageCache` is an `NSCache` based remote-image caching mechanism for iOS projects. 6 | 7 | ## Screenshots (Demo App) 8 | 9 |
10 | 11 | ## How It Works (Logically) 12 | 13 | There are three states an image can be in: 14 | 15 | * Cached In Memory 16 | * Cached On Disk 17 | * Not Cached 18 | 19 | If an image is requested from cache, and it has never been cached, it is downloaded, stored on disk, put into memory and returned via a delegate callback. 20 | 21 | If an image is requested from cache, and it has been cached, but hasn't been requested this session, it is read from disk, brought into memory and returned immediately. 22 | 23 | If an image is requested from cache, and it has been cached and it is already in memory, it is simply returned immediately. 24 | 25 | The idea behind `JMImageCache` is to always return images the **fastest** way possible, thus the in-memory caching. Reading from disk can be expensive, and should only be done if it has to be. 26 | 27 | ## How It Works (Code) 28 | 29 | The clean and easy way (uses a category that `JMImageCache` adds to `UIImageView`): 30 | 31 | ``` objective-c 32 | [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://dundermifflin.com/i/MichaelScott.png"] 33 | placeholder:[UIImage imageNamed:@"placeholder.png"]]; 34 | ``` 35 | 36 | Request an image like so: 37 | 38 | ``` objective-c 39 | [[JMImageCache sharedCache] imageForURL:[NSURL URLWithString:@"http://dundermifflin.com/i/MichaelScott.png"] completionBlock:^(UIImage *downloadedImage) { 40 | someImageView.image = downloadedImage; 41 | }]; 42 | ``` 43 | 44 | If you need more control all the methods allow to specify the key of an image. 45 | This can be used to keep track of different images associated with the same url (e.g. different border radius). 46 | This can also be used to access an image that might have been downloaded in situations where the url is not readily available. 47 | 48 | ``` objective-c 49 | [cell.imageView setImageWithURL:urlWhichMightBeNil 50 | key:@"$ImageKey" 51 | placeholder:[UIImage imageNamed:@"placeholder.png"]]; 52 | ``` 53 | 54 | ## Clearing The Cache 55 | 56 | The beauty of building on top of `NSCache` is that` JMImageCache` handles low memory situations gracefully. It will evict objects on its own when memory gets tight, you don't need to worry about it. 57 | 58 | However, if you really need to, clearing the cache manually is this simple: 59 | 60 | ``` objective-c 61 | [[JMImageCache sharedCache] removeAllObjects]; 62 | ``` 63 | 64 | If you'd like to remove a specific image from the cache, you can do this: 65 | 66 | ``` objective-c 67 | [[JMImageCache sharedCache] removeImageForURL:@"http://dundermifflin.com/i/MichaelScott.png"]; 68 | ``` 69 | 70 | ##Demo App 71 | 72 | This repository is actually a demo project itself. Just a simple `UITableViewController` app that loads a few images. Nothing too fancy, but it should give you a good idea of a standard usage of `JMImageCache`. 73 | 74 | ## Adding To Your Project 75 | 76 | ### With CocoaPods 77 | 78 | If you are using [CocoaPods](http://cocoapods.org) then just add this line to your `Podfile`: 79 | 80 | ``` ruby 81 | pod 'JMImageCache' 82 | ``` 83 | 84 | Now run `pod install` to install the dependency. 85 | 86 | ### Without CocoaPods 87 | 88 | [Download](https://github.com/jakemarsh/JMImageCache/zipball/master) the source files or add it as a [git submodule](http://schacon.github.com/git/user-manual.html#submodules). Here's how to add it as a submodule: 89 | 90 | $ cd YourProject 91 | $ git submodule add https://github.com/jakemarsh/JMImageCache.git Vendor/JMImageCache 92 | 93 | Add the 4 Objective-C files inside the `JMImageCache` folder to your project. `#import "JMImageCache.h" where you need it.` 94 | 95 | ## ARC (Automatic Reference Counting) 96 | 97 | `JMImageCache` uses [Automatic Reference Counting (ARC)](http://clang.llvm.org/docs/AutomaticReferenceCounting.html). If your project doesn't use ARC, you will need to set the `-fobjc-arc` compiler flag on all of the `JMImageCache` source files. To do this in Xcode, go to your active target and select the "Build Phases" tab. In the "Compiler Flags" column, set `-fobjc-arc` for each of the `JMImageCache` source files. -------------------------------------------------------------------------------- /UIImageView+JMImageCache.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIImageView+JMImageCache.h 3 | // JMImageCacheDemo 4 | // 5 | // Created by Jake Marsh on 7/23/12. 6 | // Copyright (c) 2012 Jake Marsh. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIImageView (JMImageCache) 12 | 13 | - (void) setImageWithURL:(NSURL *)url; 14 | - (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage; 15 | - (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *))completionBlock; 16 | - (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *))completionBlock failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure; 17 | - (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage; 18 | - (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *image))completionBlock; 19 | - (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *image))completionBlock failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure; 20 | 21 | 22 | @end -------------------------------------------------------------------------------- /UIImageView+JMImageCache.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIImageView+JMImageCache.m 3 | // JMImageCacheDemo 4 | // 5 | // Created by Jake Marsh on 7/23/12. 6 | // Copyright (c) 2012 Jake Marsh. All rights reserved. 7 | // 8 | 9 | #import "UIImageView+JMImageCache.h" 10 | #import "JMImageCache.h" 11 | #import 12 | 13 | static char kJMImageURLObjectKey; 14 | 15 | @interface UIImageView (_JMImageCache) 16 | 17 | @property (readwrite, nonatomic, retain, setter = jm_setImageURL:) NSURL *jm_imageURL; 18 | 19 | @end 20 | 21 | @implementation UIImageView (_JMImageCache) 22 | 23 | @dynamic jm_imageURL; 24 | 25 | @end 26 | 27 | @implementation UIImageView (JMImageCache) 28 | 29 | #pragma mark - Private Setters 30 | 31 | - (NSURL *) jm_imageURL { 32 | return (NSURL *)objc_getAssociatedObject(self, &kJMImageURLObjectKey); 33 | } 34 | - (void) jm_setImageURL:(NSURL *)imageURL { 35 | objc_setAssociatedObject(self, &kJMImageURLObjectKey, imageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 36 | } 37 | 38 | #pragma mark - Public Methods 39 | 40 | - (void) setImageWithURL:(NSURL *)url { 41 | [self setImageWithURL:url placeholder:nil]; 42 | } 43 | - (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage { 44 | [self setImageWithURL:url key:nil placeholder:placeholderImage]; 45 | } 46 | - (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *))completionBlock { 47 | [self setImageWithURL:url key:nil placeholder:placeholderImage completionBlock:completionBlock failureBlock:nil]; 48 | } 49 | - (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *))completionBlock failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failureBlock { 50 | [self setImageWithURL:url key:nil placeholder:placeholderImage completionBlock:completionBlock failureBlock:failureBlock]; 51 | } 52 | - (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage { 53 | [self setImageWithURL:url key:key placeholder:placeholderImage completionBlock:nil]; 54 | } 55 | - (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *image))completionBlock { 56 | [self setImageWithURL:url key:key placeholder:placeholderImage completionBlock:completionBlock]; 57 | } 58 | - (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *image))completionBlock failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failureBlock{ 59 | self.jm_imageURL = url; 60 | self.image = placeholderImage; 61 | 62 | [self setNeedsDisplay]; 63 | [self setNeedsLayout]; 64 | 65 | __weak UIImageView *safeSelf = self; 66 | 67 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 68 | UIImage *i; 69 | 70 | if (key) { 71 | i = [[JMImageCache sharedCache] cachedImageForKey:key]; 72 | } else { 73 | i = [[JMImageCache sharedCache] cachedImageForURL:url]; 74 | } 75 | 76 | if(i) { 77 | dispatch_async(dispatch_get_main_queue(), ^{ 78 | safeSelf.jm_imageURL = nil; 79 | 80 | safeSelf.image = i; 81 | 82 | [safeSelf setNeedsLayout]; 83 | [safeSelf setNeedsDisplay]; 84 | }); 85 | } else { 86 | dispatch_async(dispatch_get_main_queue(), ^{ 87 | safeSelf.image = placeholderImage; 88 | 89 | [safeSelf setNeedsDisplay]; 90 | [safeSelf setNeedsLayout]; 91 | }); 92 | 93 | [[JMImageCache sharedCache] imageForURL:url key:key completionBlock:^(UIImage *image) { 94 | if ([url isEqual:safeSelf.jm_imageURL]) { 95 | dispatch_async(dispatch_get_main_queue(), ^{ 96 | if(image) { 97 | safeSelf.image = image; 98 | } else { 99 | safeSelf.image = placeholderImage; 100 | } 101 | 102 | safeSelf.jm_imageURL = nil; 103 | 104 | [safeSelf setNeedsLayout]; 105 | [safeSelf setNeedsDisplay]; 106 | 107 | if (completionBlock) completionBlock(image); 108 | }); 109 | } 110 | } 111 | failureBlock:^(NSURLRequest *request, NSURLResponse *response, NSError* error) 112 | { 113 | if (failureBlock) failureBlock(request, response, error); 114 | }]; 115 | } 116 | }); 117 | } 118 | 119 | @end --------------------------------------------------------------------------------