diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.h b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.h index 9d97932..0289b31 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.h +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.h @@ -20,6 +20,9 @@ typedef void(^MVMCoreGetImageBlock)(UIImage * _Nullable, NSData * _Nullable, BOO @property (nullable, strong, nonatomic, readonly) NSDictionary *staticStringCache; @property (nonnull, strong, nonatomic) NSOperationQueue *completionQueue; +/// Contains all bundles to search for images. +@property (nonnull, strong, nonatomic) NSMutableArray* imageBundles; + // Returns the shared instance of this singleton + (nullable instancetype)sharedCache; @@ -108,8 +111,14 @@ typedef void(^MVMCoreGetImageBlock)(UIImage * _Nullable, NSData * _Nullable, BOO #pragma mark Image Functions -// Subclass to return the bundle to use -- (nonnull NSBundle *)bundleToUseForImages; +/// Register a bundle as one to search for images in. +- (void)registerBundleForImages:(nonnull NSBundle *)bundle; + +/// Tries to create an image from a registered bundle. +- (nullable UIImage *)getImageFromRegisteredBundles:(nonnull NSString *)imageName; + +/// Tries to get image data from a registered bundle. Can specify the type of extension if desired. +- (nullable NSData *)getImageDataFromRegisteredBundles:(nonnull NSString *)imageName type:(nullable NSString *)type; /// Returns if the imageName pointing to a local image. Currently just checks if it begins with http. + (BOOL)isHostedImage:(nullable NSString *)imageName; diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 0d552c3..7d5efbb 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -57,6 +57,8 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; self.moduleQueue.maxConcurrentOperationCount = 1; self.imageCacheQueue = dispatch_queue_create("imgCache", DISPATCH_QUEUE_CONCURRENT); + + self.imageBundles = [NSMutableArray array]; } return self; } @@ -374,8 +376,30 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; #pragma mark - Image Functions -- (nonnull NSBundle *)bundleToUseForImages { - return [MVMCoreGetterUtility bundleForMVMCore]; +- (void)registerBundleForImages:(nonnull NSBundle *)bundle { + [self.imageBundles addObject:bundle]; +} + +- (nullable UIImage *)getImageFromRegisteredBundles:(nonnull NSString *)imageName { + UIImage *image = nil; + for (NSBundle *bundle in self.imageBundles) { + image = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil]; + if (image) { + break; + } + } + return image; +} + +- (nullable NSData *)getImageDataFromRegisteredBundles:(nonnull NSString *)imageName type:(nullable NSString *)type { + NSData *imageData = nil; + for (NSBundle *bundle in self.imageBundles) { + imageData = [NSData dataWithContentsOfFile:[bundle pathForResource:imageName ofType:type]]; + if (imageData) { + break; + } + } + return imageData; } - (NSMutableDictionary *)imgCache { @@ -487,8 +511,11 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; // Try Local. Check in memory cache imageData = [self getCachedDataWithName:pathOrName]; if (!imageData) { - NSBundle *imageBundle = localBundle ? : [self bundleToUseForImages]; - imageData = [NSData dataWithContentsOfFile:[imageBundle pathForResource:pathOrName ofType:@"gif"]]; + if (localBundle) { + imageData = [NSData dataWithContentsOfFile:[localBundle pathForResource:pathOrName ofType:@"gif"]]; + } else { + imageData = [self getImageDataFromRegisteredBundles:pathOrName type:@"gif"]; + } if (imageData) { [self addDataToCache:imageData withName:pathOrName]; } @@ -525,7 +552,11 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; // Try native image. Check in memory cache image = [self getCachedImageWithName:pathOrName]; if (!image) { - image = [UIImage imageNamed:pathOrName inBundle:localBundle ?: [self bundleToUseForImages] compatibleWithTraitCollection:nil]; + if (localBundle) { + image = [UIImage imageNamed:pathOrName inBundle:localBundle compatibleWithTraitCollection:nil]; + } else { + image = [self getImageFromRegisteredBundles:pathOrName]; + } if (image) { [self addImageToCache:image withName:pathOrName]; } @@ -585,7 +616,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; image = [self getCachedImageWithName:fallbackImageName]; if (!image) { - image = [UIImage imageNamed:fallbackImageName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil]; + image = [self getImageFromRegisteredBundles:fallbackImageName]; if (image) { // Cache the fallback image if not already cached.