-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define the Image Cache delegate to be used in the ImageLoader
Reviewed By: javache Differential Revision: D3583472 fbshipit-source-id: d1f30f3f96aea7739b9f91093fbb6c92c8b9567e
- Loading branch information
Maria Mateescu
authored and
Facebook Github Bot 6
committed
Jul 28, 2016
1 parent
f12d0a2
commit 54244e1
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import "RCTImageLoader.h" | ||
|
||
@interface RCTImageCache : NSObject <RCTImageCacheDelegate> | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import "RCTImageCache.h" | ||
|
||
#import <ImageIO/ImageIO.h> | ||
|
||
#import <libkern/OSAtomic.h> | ||
|
||
#import <objc/runtime.h> | ||
|
||
#import "RCTConvert.h" | ||
#import "RCTImageUtils.h" | ||
#import "RCTNetworking.h" | ||
#import "RCTUtils.h" | ||
|
||
static const NSUInteger RCTMaxCachableDecodedImageSizeInBytes = 1048576; // 1MB | ||
|
||
static NSString *RCTCacheKeyForImage(NSString *imageTag, CGSize size, CGFloat scale, | ||
RCTResizeMode resizeMode, NSString *responseDate) | ||
{ | ||
return [NSString stringWithFormat:@"%@|%g|%g|%g|%zd|%@", | ||
imageTag, size.width, size.height, scale, resizeMode, responseDate]; | ||
} | ||
|
||
@implementation RCTImageCache | ||
{ | ||
NSOperationQueue *_imageDecodeQueue; | ||
NSCache *_decodedImageCache; | ||
} | ||
|
||
- (instancetype)init | ||
{ | ||
_decodedImageCache = [NSCache new]; | ||
_decodedImageCache.totalCostLimit = 5 * 1024 * 1024; // 5MB | ||
|
||
[[NSNotificationCenter defaultCenter] addObserver:self | ||
selector:@selector(clearCache) | ||
name:UIApplicationDidReceiveMemoryWarningNotification | ||
object:nil]; | ||
[[NSNotificationCenter defaultCenter] addObserver:self | ||
selector:@selector(clearCache) | ||
name:UIApplicationWillResignActiveNotification | ||
object:nil]; | ||
|
||
return self; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[[NSNotificationCenter defaultCenter] removeObserver:self]; | ||
} | ||
|
||
- (void)clearCache | ||
{ | ||
[_decodedImageCache removeAllObjects]; | ||
} | ||
|
||
- (void)addImageToCache:(UIImage *)image | ||
forKey:(NSString *)cacheKey | ||
{ | ||
if (!image) { | ||
return; | ||
} | ||
CGFloat bytes = image.size.width * image.size.height * image.scale * image.scale * 4; | ||
if (bytes <= RCTMaxCachableDecodedImageSizeInBytes) { | ||
[self->_decodedImageCache setObject:image | ||
forKey:cacheKey | ||
cost:bytes]; | ||
} | ||
} | ||
|
||
- (UIImage *)imageForUrl:(NSString *)url | ||
size:(CGSize)size | ||
scale:(CGFloat)scale | ||
resizeMode:(RCTResizeMode)resizeMode | ||
responseDate:(NSString *)responseDate | ||
{ | ||
NSString *cacheKey = RCTCacheKeyForImage(url, size, scale, resizeMode, responseDate); | ||
return [_decodedImageCache objectForKey:cacheKey]; | ||
} | ||
|
||
- (void)addImageToCache:(UIImage *)image | ||
URL:(NSString *)url | ||
size:(CGSize)size | ||
scale:(CGFloat)scale | ||
resizeMode:(RCTResizeMode)resizeMode | ||
responseDate:(NSString *)responseDate | ||
{ | ||
NSString *cacheKey = RCTCacheKeyForImage(url, size, scale, resizeMode, responseDate); | ||
return [self addImageToCache:image forKey:cacheKey]; | ||
} | ||
|
||
@end |