Skip to content

Instantly share code, notes, and snippets.

@alexpaul
Last active May 26, 2018 22:16
Show Gist options
  • Save alexpaul/fb2eeddbcb7fea0423f00a4623df9c15 to your computer and use it in GitHub Desktop.
Save alexpaul/fb2eeddbcb7fea0423f00a4623df9c15 to your computer and use it in GitHub Desktop.
Uses NSCache to temporarily store images using key/value.
//
// ImageCache.h
// Events
//
// Created by Alex Paul on 5/24/18.
// Copyright © 2018 Alex Paul. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ImageCache: NSObject
+ (id)sharedManager; // singleton
- (UIImage *)getImageForKey:(NSString *)key;
- (void)downloadImageWithURLString:(NSString *)urlString completionHandler:(void (^)(NSError *, UIImage *))completion;
@end
//
// ImageCache.m
// Events
//
// Created by Alex Paul on 5/24/18.
// Copyright © 2018 Alex Paul. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ImageCache.h"
#import "NetworkHelper.h"
@interface ImageCache ()
// NSCache stores objects to the caches directory. This prevents the redownloading of existing assets.
@property (nonatomic) NSCache *sharedCache;
@property (nonatomic) NSMutableURLRequest *urlRequest;
@end
@implementation ImageCache
// Singleton pattern in Objective-C
+ (instancetype)sharedManager {
static ImageCache *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
- (instancetype)init {
self = [super init];
if (self) {
// initialize properties here...
_sharedCache = [[NSCache alloc] init];
}
return self;
}
- (void)cacheImage:(UIImage *)image forKey:(NSString *)key {
[self.sharedCache setObject:image forKey:key];
}
- (UIImage *)getImageForKey:(NSString *)key {
return [self.sharedCache objectForKey:key];
}
- (void)downloadImageWithURLString:(NSString *)urlString completionHandler:(void (^)(NSError *, UIImage *))completion {
if (!_urlRequest)
_urlRequest = [[NSMutableURLRequest alloc] init];
self.urlRequest.URL = [NSURL URLWithString:urlString];
[[NetworkHelper sharedManager] performRequestWithRequest:self.urlRequest completionHandler:^(NSError *error, NSData *data) {
if (error) {
completion(error, nil);
} else {
UIImage *image = [[UIImage alloc] initWithData:data];
[self cacheImage:image forKey:urlString];
completion(nil, image);
}
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment