Skip to content

Commit

Permalink
IOS-3384 enable CLCG to load cached images more quickly to allow for …
Browse files Browse the repository at this point in the history
…faster perceived loading of book page. Enable loading the best cached image directly in CLCGImageView
  • Loading branch information
charleki authored and ettore committed Dec 3, 2015
1 parent d530bca commit 7095dfb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
12 changes: 12 additions & 0 deletions CLCGImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ typedef void (^CLCGImageLoaderCallback)(UIImage *img, int http_status);

@property(nonatomic,retain,readonly) NSCache *cache;

/*!
Singleton instance of the CLCGImageLoader
*/
+(CLCGImageLoader*)i;

/*!
@return The largest size image currently stored in cache.
*/
+(UIImage*)bestCachedImageForURL:(NSString*)normal_url
retinaURL:(NSString*)retina_url
retinaHDURL:(NSString*)retina_hd_url;

/*!
@deprecated
*/
Expand Down
50 changes: 40 additions & 10 deletions CLCGImageLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,11 @@ +(ASIHTTPRequest*)loadImageForURL:(NSString*)normal_url





+(ASIHTTPRequest*)loadImageForURL:(NSString*)normal_url
retinaURL:(NSString*)retina_url
retinaHDURL:(NSString*)retina_hd_url
useCache:(BOOL)use_cache
block:(CLCGImageLoaderCallback)block
+(NSURL*)screenImageURLForURL:(NSString*)normal_url
retinaURL:(NSString*)retina_url
retinaHDURL:(NSString*)retina_hd_url
{
NSURL *url = nil;
__block ASIHTTPRequest *req = nil;
const CGFloat screen_scale = [UIScreen mainScreen].scale;
const BOOL is_retina_hd = (screen_scale >= 3.0);
const BOOL is_retina = (screen_scale >= 2.0);
Expand All @@ -109,7 +104,42 @@ +(ASIHTTPRequest*)loadImageForURL:(NSString*)normal_url
&& !clcg_str_isblank(normal_url)) {
url = [[NSURL alloc] initWithString:normal_url];
}
return url;
}


+(UIImage*)bestCachedImageForURL:(NSString*)normal_url
retinaURL:(NSString*)retina_url
retinaHDURL:(NSString*)retina_hd_url
{
UIImage *img = [[[CLCGImageLoader i] cache] objectForKey:retina_hd_url];

if (img == nil) {
img = [[[CLCGImageLoader i] cache] objectForKey:retina_url];
}

if (img == nil) {
img = [[[CLCGImageLoader i] cache] objectForKey:normal_url];
}

if (img) {
return img;
}

return nil;
}


+(ASIHTTPRequest*)loadImageForURL:(NSString*)normal_url
retinaURL:(NSString*)retina_url
retinaHDURL:(NSString*)retina_hd_url
useCache:(BOOL)use_cache
block:(CLCGImageLoaderCallback)block
{
__block ASIHTTPRequest *req = nil;
NSURL *url = [CLCGImageLoader screenImageURLForURL:normal_url
retinaURL:retina_url
retinaHDURL:retina_hd_url];
if (url) {
block = [block copy];//make sure it's on the heap

Expand All @@ -120,7 +150,6 @@ +(ASIHTTPRequest*)loadImageForURL:(NSString*)normal_url
return nil;
}
}

// configure the request
req = [[ASIHTTPRequest alloc] initWithURL:url];
[req addRequestHeader:@"Accept-Encoding" value:@"gzip"];
Expand All @@ -133,11 +162,12 @@ +(ASIHTTPRequest*)loadImageForURL:(NSString*)normal_url
NSData *data = [req responseData];
// TODO: we should likely use imageWithData:scale: instead.
UIImage *img = [UIImage imageWithData:data];

if (img == nil) {
[CLCGImageLoader reportErrorForRequest:req block:block];
} else {
if (use_cache) {
[[[CLCGImageLoader i] cache] setObject:img
[[[CLCGImageLoader i] cache] setObject:img
forKey:[[req originalURL] absoluteString]];
}
[self returnImage:img status:[req responseStatusCode] block:block];
Expand Down
4 changes: 4 additions & 0 deletions CLCGImageView.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ typedef void (^CLCGImageViewOnLoadCallback)(UIImage *img, int http_status);
retinaURL:(NSString*)retina_url
retinaHDURL:(NSString*)retina_hd_url;

-(void)loadCachedImageIfPossibleForURL:(NSString*)normal_url
retinaURL:(NSString*)retina_url
retinaHDURL:(NSString*)retina_hd_url;

@end
15 changes: 15 additions & 0 deletions CLCGImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ -(void)loadImageForURL:(NSString*)normal_url
retinaHDURL:nil];
}


-(void)loadCachedImageIfPossibleForURL:(NSString*)normal_url
retinaURL:(NSString*)retina_url
retinaHDURL:(NSString*)retina_hd_url
{
UIImage *img = [CLCGImageLoader bestCachedImageForURL:normal_url
retinaURL:retina_url
retinaHDURL:retina_hd_url];
if (img) {
[self setImage:img];
return;
}
}


-(void)loadImageForURL:(NSString*)normal_url
retinaURL:(NSString*)retina_url
retinaHDURL:(NSString*)retina_hd_url
Expand Down

0 comments on commit 7095dfb

Please sign in to comment.