diff --git a/YBImageBrowser/Image/YBIBImageCache.h b/YBImageBrowser/Image/YBIBImageCache.h index 0b8591f..428a1a5 100644 --- a/YBImageBrowser/Image/YBIBImageCache.h +++ b/YBImageBrowser/Image/YBIBImageCache.h @@ -10,10 +10,17 @@ NS_ASSUME_NONNULL_BEGIN -@interface YBIBImageCache : NSObject +@class YBIBImageCache; + +@interface NSObject (YBIBImageCache) -/// 唯一单例 -+ (instancetype)sharedCache; +/// 图片浏览器持有的图片缓存管理类 +@property (nonatomic, strong, readonly) YBIBImageCache *ybib_imageCache; + +@end + + +@interface YBIBImageCache : NSObject /// 缓存数量限制(一个单位表示一个 YBIBImageData 产生的所有图片数据) @property (nonatomic, assign) NSUInteger imageCacheCountLimit; diff --git a/YBImageBrowser/Image/YBIBImageCache.m b/YBImageBrowser/Image/YBIBImageCache.m index 66748aa..ce46b51 100644 --- a/YBImageBrowser/Image/YBIBImageCache.m +++ b/YBImageBrowser/Image/YBIBImageCache.m @@ -9,6 +9,24 @@ #import "YBIBImageCache.h" #import "YBIBImageCache+Internal.h" #import "YBIBUtilities.h" +#import + + +@implementation NSObject (YBIBImageCache) +static void *YBIBImageCacheKey = &YBIBImageCacheKey; +- (void)setYbib_imageCache:(YBIBImageCache *)ybib_imageCache { + objc_setAssociatedObject(self, YBIBImageCacheKey, ybib_imageCache, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} +- (YBIBImageCache *)ybib_imageCache { + YBIBImageCache *cache = objc_getAssociatedObject(self, YBIBImageCacheKey); + if (!cache) { + cache = [YBIBImageCache new]; + self.ybib_imageCache = cache; + } + return cache; +} +@end + @interface YBIBImageCachePack : NSObject @property (nonatomic, strong) UIImage *originImage; @@ -25,15 +43,6 @@ @implementation YBIBImageCache { #pragma mark - life cycle -+ (instancetype)sharedCache { - static YBIBImageCache *cache; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - cache = [YBIBImageCache new]; - }); - return cache; -} - - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; } diff --git a/YBImageBrowser/Image/YBIBImageData.m b/YBImageBrowser/Image/YBIBImageData.m index e9a3df3..e49d94d 100644 --- a/YBImageBrowser/Image/YBIBImageData.m +++ b/YBImageBrowser/Image/YBIBImageData.m @@ -42,7 +42,7 @@ - (void)dealloc { if (_downloadToken) { [YBIBWebImageManager cancelTaskWithDownloadToken:_downloadToken]; } - [YBIBImageCache.sharedCache removeForKey:self.cacheKey]; + [self.imageCache removeForKey:self.cacheKey]; } - (instancetype)init { @@ -270,7 +270,7 @@ - (void)loadURL_download { self.loadingStatus = YBIBImageLoadingStatusDownloading; __weak typeof(self) wSelf = self; _downloadToken = [YBIBWebImageManager downloadImageWithURL:self.imageURL requestModifier:^NSURLRequest * _Nullable(NSURLRequest * _Nonnull request) { - return self.requestModifier ? self.requestModifier(self, request) : nil; + return self.requestModifier ? self.requestModifier(self, request) : request; } progress:^(NSInteger receivedSize, NSInteger expectedSize) { CGFloat progress = receivedSize * 1.0 / expectedSize ?: 0; YBIB_DISPATCH_ASYNC_MAIN(^{ @@ -445,7 +445,7 @@ - (void)stopLoading { } - (void)clearCache { - [YBIBImageCache.sharedCache removeForKey:self.cacheKey]; + [self.imageCache removeForKey:self.cacheKey]; } #pragma mark - private @@ -536,6 +536,10 @@ - (void)UIImageWriteToSavedPhotosAlbum_completedWithImage:(UIImage *)image error [self saveToPhotoAlbumCompleteWithError:error]; } +- (YBIBImageCache *)imageCache { + return self.yb_backView.ybib_imageCache; +} + #pragma mark - @synthesize yb_isTransitioning = _yb_isTransitioning; @@ -543,6 +547,7 @@ - (void)UIImageWriteToSavedPhotosAlbum_completedWithImage:(UIImage *)image error @synthesize yb_containerSize = _yb_containerSize; @synthesize yb_containerView = _yb_containerView; @synthesize yb_auxiliaryViewHandler = _yb_auxiliaryViewHandler; +@synthesize yb_backView = _yb_backView; - (nonnull Class)yb_classOfCell { return YBIBImageCell.self; @@ -609,7 +614,7 @@ - (void)setDelegate:(id)delegate { } else { _freezing = YES; // Remove the resident cache if '_delegate' is nil. - [YBIBImageCache.sharedCache removeResidentForKey:self.cacheKey]; + [self.imageCache removeResidentForKey:self.cacheKey]; } } - (id)delegate { @@ -627,18 +632,18 @@ - (NSString *)cacheKey { - (void)setOriginImage:(__kindof UIImage *)originImage { // 'image' should be resident if '_delegate' exists. - [YBIBImageCache.sharedCache setImage:originImage type:YBIBImageCacheTypeOrigin forKey:self.cacheKey resident:self->_delegate != nil]; + [self.imageCache setImage:originImage type:YBIBImageCacheTypeOrigin forKey:self.cacheKey resident:self->_delegate != nil]; } - (UIImage *)originImage { - return [YBIBImageCache.sharedCache imageForKey:self.cacheKey type:YBIBImageCacheTypeOrigin]; + return [self.imageCache imageForKey:self.cacheKey type:YBIBImageCacheTypeOrigin]; } - (void)setCompressedImage:(UIImage *)compressedImage { // 'image' should be resident if '_delegate' exists. - [YBIBImageCache.sharedCache setImage:compressedImage type:YBIBImageCacheTypeCompressed forKey:self.cacheKey resident:_delegate != nil]; + [self.imageCache setImage:compressedImage type:YBIBImageCacheTypeCompressed forKey:self.cacheKey resident:_delegate != nil]; } - (UIImage *)compressedImage { - return [YBIBImageCache.sharedCache imageForKey:self.cacheKey type:YBIBImageCacheTypeCompressed]; + return [self.imageCache imageForKey:self.cacheKey type:YBIBImageCacheTypeCompressed]; } - (void)setLoadingStatus:(YBIBImageLoadingStatus)loadingStatus {