Skip to content

Commit

Permalink
[feautre] 修复下载时的BUG;优化图片内存管理策略
Browse files Browse the repository at this point in the history
  • Loading branch information
indulgeIn committed Jul 17, 2019
1 parent 8f1a711 commit b0a9926
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
13 changes: 10 additions & 3 deletions YBImageBrowser/Image/YBIBImageCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 18 additions & 9 deletions YBImageBrowser/Image/YBIBImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
#import "YBIBImageCache.h"
#import "YBIBImageCache+Internal.h"
#import "YBIBUtilities.h"
#import <objc/runtime.h>


@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;
Expand All @@ -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];
}
Expand Down
21 changes: 13 additions & 8 deletions YBImageBrowser/Image/YBIBImageData.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ - (void)dealloc {
if (_downloadToken) {
[YBIBWebImageManager cancelTaskWithDownloadToken:_downloadToken];
}
[YBIBImageCache.sharedCache removeForKey:self.cacheKey];
[self.imageCache removeForKey:self.cacheKey];
}

- (instancetype)init {
Expand Down Expand Up @@ -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(^{
Expand Down Expand Up @@ -445,7 +445,7 @@ - (void)stopLoading {
}

- (void)clearCache {
[YBIBImageCache.sharedCache removeForKey:self.cacheKey];
[self.imageCache removeForKey:self.cacheKey];
}

#pragma mark - private
Expand Down Expand Up @@ -536,13 +536,18 @@ - (void)UIImageWriteToSavedPhotosAlbum_completedWithImage:(UIImage *)image error
[self saveToPhotoAlbumCompleteWithError:error];
}

- (YBIBImageCache *)imageCache {
return self.yb_backView.ybib_imageCache;
}

#pragma mark - <YBIBDataProtocol>

@synthesize yb_isTransitioning = _yb_isTransitioning;
@synthesize yb_currentOrientation = _yb_currentOrientation;
@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;
Expand Down Expand Up @@ -609,7 +614,7 @@ - (void)setDelegate:(id<YBIBImageDataDelegate>)delegate {
} else {
_freezing = YES;
// Remove the resident cache if '_delegate' is nil.
[YBIBImageCache.sharedCache removeResidentForKey:self.cacheKey];
[self.imageCache removeResidentForKey:self.cacheKey];
}
}
- (id<YBIBImageDataDelegate>)delegate {
Expand All @@ -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 {
Expand Down

0 comments on commit b0a9926

Please sign in to comment.