Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

it's more powerful #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions Demo/Classes/DemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,80 @@

@interface DemoViewController ()

@property (strong, nonatomic) NSMutableArray *modelArray;
@property (copy, nonatomic) NSArray *modelArray;

@end

@implementation DemoViewController

static NSString *CellIdentifier = @"Cell"; // cell identifier

@synthesize modelArray = _modelArray;

- (id) init {
- (instancetype) init {
self = [super init];
if(!self) return nil;

self.title = @"The Office";

self.modelArray = [[NSMutableArray alloc] init];

[self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4hCR/Untitled-7.png", @"ImageURL", @"Michael Scott", @"Title", nil]];
[self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4iIc/Untitled-7.png", @"ImageURL", @"Jim Halpert", @"Title", nil]];
[self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4hVv/Untitled-7.png", @"ImageURL", @"Pam Beasley-Halpert", @"Title", nil]];
[self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4hF3/Untitled-7.png", @"ImageURL", @"Dwight Schrute", @"Title", nil]];
[self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4hxj/Untitled-7.png", @"ImageURL", @"Andy Bernard", @"Title", nil]];
[self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4iNI/Untitled-7.png", @"ImageURL", @"Kevin Malone", @"Title", nil]];
[self.modelArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"http://cl.ly/4iAX/Untitled-7.png", @"ImageURL", @"Stanley Hudson", @"Title", nil]];
self.modelArray = @[@{@"ImageURL" : @"http://cl.ly/4hCR/Untitled-7.png", @"Title" : @"Michael Scott"},
@{@"ImageURL" : @"http://cl.ly/4iIc/Untitled-7.png", @"Title" : @"Jim Halpert"},
@{@"ImageURL" : @"http://cl.ly/4hVv/Untitled-7.png", @"Title" : @"Pam Beasley-Halpert"},
@{@"ImageURL" : @"http://cl.ly/4hF3/Untitled-7.png", @"Title" : @"Dwight Schrute"},
@{@"ImageURL" : @"http://cl.ly/4hxj/Untitled-7.png", @"Title" : @"Andy Bernard"},
@{@"ImageURL" : @"http://cl.ly/4iNI/Untitled-7.png", @"Title" : @"Kevin Malone"},
@{@"ImageURL" : @"http://cl.ly/4iAX/Untitled-7.png", @"Title" : @"Stanley Hudson"}];

// 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];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];

return self;
}

#pragma mark -
#pragma mark Autorotation Methods
#pragma mark - Autorotation Methods

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}

#pragma mark -
#pragma mark Cleanup Methods
#pragma mark - Cleanup Methods

- (void) didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

#pragma mark -
#pragma mark UITableViewDataSource
#pragma mark - UITableViewDataSource

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.modelArray count];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

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"]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSDictionary *cellDic = (self.modelArray)[indexPath.row];
cell.textLabel.text = cellDic[@"Title"];

JMImageCacheDownloadOptions option = JMImageCacheDownloadOptionsClickToDownload;
// option |= JMImageCacheDownloadOptionsClickToRefresh;
option |= JMImageCacheDownloadOptionsSearchCacheOnly;
[cell.imageView setImageWithURL:[NSURL URLWithString:cellDic[@"ImageURL"]]
key:nil
options:option
placeholder:[UIImage imageNamed:@"placeholder"]
completionBlock:^(UIImage *image) {
//
} failureBlock:^(NSURLRequest *request, NSURLResponse *response, NSError *error) {
//
}];

return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
#pragma mark - UITableViewDelegate

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 90.0;
Expand Down
2 changes: 2 additions & 0 deletions Demo/JMImageCacheDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ENABLE_OBJC_ARC = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand All @@ -241,6 +242,7 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ENABLE_OBJC_ARC = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
COPY_PHASE_STRIP = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Other Sources/JMImageCacheDemo_Prefix.pch";
Expand Down
31 changes: 8 additions & 23 deletions JMImageCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,23 @@

#import "UIImageView+JMImageCache.h"

@class JMImageCache;

@protocol JMImageCacheDelegate <NSObject>

@optional
- (void) cache:(JMImageCache *)c didDownloadImage:(UIImage *)i forURL:(NSURL *)url;
- (void) cache:(JMImageCache *)c didDownloadImage:(UIImage *)i forURL:(NSURL *)url key:(NSString*)key;

@end

@interface JMImageCache : NSCache

+ (JMImageCache *) sharedCache;

- (void) imageForURL:(NSURL *)url key:(NSString *)key completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure;
- (void) imageForURL:(NSURL *)url completionBlock:(void (^)(UIImage *image))completion failureBlock:(void (^)(NSURLRequest *request, NSURLResponse *response, NSError* error))failure;

- (UIImage *) cachedImageForKey:(NSString *)key;
- (UIImage *) cachedImageForURL:(NSURL *)url;
- (void) imageForURL:(NSURL *)url key:(NSString *)key completionBlock:(JMICCompletionBlock)completion failureBlock:(JMICFailureBlock)failure;
- (void) imageForURL:(NSURL *)url completionBlock:(JMICCompletionBlock)completion failureBlock:(JMICFailureBlock)failure;

- (UIImage *) imageForURL:(NSURL *)url key:(NSString*)key delegate:(id<JMImageCacheDelegate>)d;
- (UIImage *) imageForURL:(NSURL *)url delegate:(id<JMImageCacheDelegate>)d;
- (UIImage *) cachedImageForURL:(NSURL *)url key:(NSString *)key;;

- (UIImage *) imageFromDiskForKey:(NSString *)key;
- (UIImage *) imageFromDiskForURL:(NSURL *)url;
- (UIImage *) imageFromDiskForURL:(NSURL *)url key:(NSString *)key;;

- (void) setImage:(UIImage *)i forKey:(NSString *)key;
- (void) setImage:(UIImage *)i forURL:(NSURL *)url;
- (void) removeImageForKey:(NSString *)key;
- (void) removeImageForURL:(NSURL *)url;
- (void) setImage:(UIImage *)i forURL:(NSURL *)url key:(NSString *)key;
- (void) removeImageForURL:(NSURL *)url key:(NSString *)key;

- (void) writeData:(NSData *)data toPath:(NSString *)path;
- (void) performDiskWriteOperation:(NSInvocation *)invoction;

- (void) _downloadAndWriteImageForURL:(NSURL *)url key:(NSString *)key completionBlock:(JMICCompletionBlock)completion failureBlock:(JMICFailureBlock)failure;

@end
Loading