mfcache image block return bool to indicate if the image is fall back image

This commit is contained in:
Yang, Tianhang(Chris) 2018-02-09 11:06:38 -05:00
parent 835808843f
commit 09463532bf
2 changed files with 19 additions and 15 deletions

View File

@ -10,6 +10,10 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
//block returned when getting image
//parameters are UIImage object for the image, NSData for gif images, UIImage object for the image, A BOOL to indicate if it is a fall back image.
typedef void(^MVMCoreGetImageBlock)(UIImage * _Nullable, NSData * _Nullable, BOOL);
@interface MVMCoreCache : NSObject
// The static string cache. Stored on the device.
@ -114,12 +118,12 @@
// Most cases only need to provide the width. If both are provided and the aspect ratio is different from the images, it will center the image.
// In the future, may need to use align for the shop flow.
// localFallbackImageName: should be the name of a local file to use in case of failure to download.
- (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull void (^)(UIImage * _Nullable))completionHandler;
- (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull void (^)(UIImage * _Nullable))completionHandler;
- (void)getGif:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull void (^)(NSData * _Nullable, UIImage * _Nullable))completionHandler;
- (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler;
- (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler;
- (void)getGif:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler;
// Gets a cropped version of the requested image. Similar to the method above, but you can specify the final rect
- (void)getCroppedImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 finalRect:(CGRect)finalRect flipImage:(BOOL)flipImage localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull void (^)(UIImage * _Nullable))completionHandler;
- (void)getCroppedImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 finalRect:(CGRect)finalRect flipImage:(BOOL)flipImage localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler;
// Gets an image from the cache.
- (nullable UIImage *)getCachedImageWithName:(nonnull NSString *)imageName;

View File

@ -445,18 +445,18 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
return [components URL];
}
- (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull void (^)(UIImage * _Nullable))completionHandler {
- (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler {
[self getImage:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:nil localFallbackImageName:fallbackImageName completionHandler:completionHandler];
}
- (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull void (^)(UIImage * _Nullable))completionHandler {
- (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler {
[self getImage:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:CGRectNull flipImage:NO localFallbackImageName:fallbackImageName completionHandler:completionHandler];
}
- (void)getGif:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull void (^)(NSData * _Nullable, UIImage * _Nullable))completionHandler {
- (void)getGif:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
BOOL isFallBackImage = NO;
NSData *imageData = nil;
if (pathOrName.length > 0) {
@ -493,7 +493,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
// Set to the fallback image.
UIImage *fallbackImage = nil;
if (!imageData && fallbackImageName) {
isFallBackImage = YES;
// Grab fallback from cache
fallbackImage = [self getCachedImageWithName:fallbackImageName];
if (!fallbackImage) {
@ -507,18 +507,18 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
}
}
completionHandler(imageData,fallbackImage);
completionHandler(fallbackImage, imageData, isFallBackImage);
});
}
- (void)getCroppedImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 finalRect:(CGRect)finalRect flipImage:(BOOL)flip localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull void (^)(UIImage * _Nullable))completionHandler {
- (void)getCroppedImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 finalRect:(CGRect)finalRect flipImage:(BOOL)flip localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler {
[self getImage:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:nil finalRect:finalRect flipImage:flip localFallbackImageName:fallbackImageName completionHandler:completionHandler];
}
- (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format finalRect:(CGRect)finalRect flipImage:(BOOL)flip localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull void (^)(UIImage * _Nullable))completionHandler {
- (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format finalRect:(CGRect)finalRect flipImage:(BOOL)flip localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
BOOL isFallBackImage = NO;
UIImage *img = nil;
if (pathOrName.length > 0) {
@ -557,7 +557,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
// Set to the fallback image.
if (!img && fallbackImageName) {
isFallBackImage = YES;
// Grab fallback from cache
img = [self getCachedImageWithName:fallbackImageName];
if (!img) {
@ -570,7 +570,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
}
}
}
completionHandler(img);
completionHandler(img, nil, isFallBackImage);
});
}