diff --git a/SDWebImage/SDImageCachesManager.m b/SDWebImage/SDImageCachesManager.m index 18657853..e8d8d319 100644 --- a/SDWebImage/SDImageCachesManager.m +++ b/SDWebImage/SDImageCachesManager.m @@ -18,7 +18,7 @@ @implementation SDImageCachesManager { - NSMutableArray> *caches_; + NSMutableArray> *_imageCaches; } + (SDImageCachesManager *)sharedManager { @@ -39,7 +39,7 @@ self.containsOperationPolicy = SDImageCachesManagerOperationPolicySerial; self.clearOperationPolicy = SDImageCachesManagerOperationPolicyConcurrent; // initialize with default image caches - caches_ = [NSMutableArray arrayWithObject:[SDImageCache sharedImageCache]]; + _imageCaches = [NSMutableArray arrayWithObject:[SDImageCache sharedImageCache]]; _cachesLock = dispatch_semaphore_create(1); } return self; @@ -47,16 +47,16 @@ - (NSArray> *)caches { SD_LOCK(self.cachesLock); - NSArray> *caches = [caches_ copy]; + NSArray> *caches = [_imageCaches copy]; SD_UNLOCK(self.cachesLock); return caches; } - (void)setCaches:(NSArray> *)caches { SD_LOCK(self.cachesLock); - [caches_ removeAllObjects]; + [_imageCaches removeAllObjects]; if (caches.count) { - [caches_ addObjectsFromArray:caches]; + [_imageCaches addObjectsFromArray:caches]; } SD_UNLOCK(self.cachesLock); } @@ -68,7 +68,7 @@ return; } SD_LOCK(self.cachesLock); - [caches_ addObject:cache]; + [_imageCaches addObject:cache]; SD_UNLOCK(self.cachesLock); } @@ -77,7 +77,7 @@ return; } SD_LOCK(self.cachesLock); - [caches_ removeObject:cache]; + [_imageCaches removeObject:cache]; SD_UNLOCK(self.cachesLock); } diff --git a/SDWebImage/SDImageCodersManager.h b/SDWebImage/SDImageCodersManager.h index cd77091a..14b655da 100644 --- a/SDWebImage/SDImageCodersManager.h +++ b/SDWebImage/SDImageCodersManager.h @@ -39,7 +39,7 @@ /** All coders in coders manager. The coders array is a priority queue, which means the later added coder will have the highest priority */ -@property (nonatomic, copy, readwrite, nullable) NSArray> *coders; +@property (nonatomic, copy, nullable) NSArray> *coders; /** Add a new coder to the end of coders array. Which has the highest priority. diff --git a/SDWebImage/SDImageCodersManager.m b/SDWebImage/SDImageCodersManager.m index e358542b..2a25fdb2 100644 --- a/SDWebImage/SDImageCodersManager.m +++ b/SDWebImage/SDImageCodersManager.m @@ -18,6 +18,9 @@ @end @implementation SDImageCodersManager +{ + NSMutableArray> *_imageCoders; +} + (nonnull instancetype)sharedManager { static dispatch_once_t once; @@ -31,12 +34,30 @@ - (instancetype)init { if (self = [super init]) { // initialize with default coders - _coders = @[[SDImageIOCoder sharedCoder], [SDImageGIFCoder sharedCoder], [SDImageAPNGCoder sharedCoder]]; + _imageCoders = [NSMutableArray arrayWithArray:@[[SDImageIOCoder sharedCoder], [SDImageGIFCoder sharedCoder], [SDImageAPNGCoder sharedCoder]]]; _codersLock = dispatch_semaphore_create(1); } return self; } +- (NSArray> *)coders +{ + SD_LOCK(self.codersLock); + NSArray> *coders = [_imageCoders copy]; + SD_UNLOCK(self.codersLock); + return coders; +} + +- (void)setCoders:(NSArray> *)coders +{ + SD_LOCK(self.codersLock); + [_imageCoders removeAllObjects]; + if (coders.count) { + [_imageCoders addObjectsFromArray:coders]; + } + SD_UNLOCK(self.codersLock); +} + #pragma mark - Coder IO operations - (void)addCoder:(nonnull id)coder { @@ -44,12 +65,7 @@ return; } SD_LOCK(self.codersLock); - NSMutableArray> *mutableCoders = [self.coders mutableCopy]; - if (!mutableCoders) { - mutableCoders = [NSMutableArray array]; - } - [mutableCoders addObject:coder]; - self.coders = [mutableCoders copy]; + [_imageCoders addObject:coder]; SD_UNLOCK(self.codersLock); } @@ -58,17 +74,13 @@ return; } SD_LOCK(self.codersLock); - NSMutableArray> *mutableCoders = [self.coders mutableCopy]; - [mutableCoders removeObject:coder]; - self.coders = [mutableCoders copy]; + [_imageCoders removeObject:coder]; SD_UNLOCK(self.codersLock); } #pragma mark - SDImageCoder - (BOOL)canDecodeFromData:(NSData *)data { - SD_LOCK(self.codersLock); NSArray> *coders = self.coders; - SD_UNLOCK(self.codersLock); for (id coder in coders.reverseObjectEnumerator) { if ([coder canDecodeFromData:data]) { return YES; @@ -78,9 +90,7 @@ } - (BOOL)canEncodeToFormat:(SDImageFormat)format { - SD_LOCK(self.codersLock); NSArray> *coders = self.coders; - SD_UNLOCK(self.codersLock); for (id coder in coders.reverseObjectEnumerator) { if ([coder canEncodeToFormat:format]) { return YES; @@ -94,9 +104,7 @@ return nil; } UIImage *image; - SD_LOCK(self.codersLock); NSArray> *coders = self.coders; - SD_UNLOCK(self.codersLock); for (id coder in coders.reverseObjectEnumerator) { if ([coder canDecodeFromData:data]) { image = [coder decodedImageWithData:data options:options]; @@ -111,9 +119,7 @@ if (!image) { return nil; } - SD_LOCK(self.codersLock); NSArray> *coders = self.coders; - SD_UNLOCK(self.codersLock); for (id coder in coders.reverseObjectEnumerator) { if ([coder canEncodeToFormat:format]) { return [coder encodedDataWithImage:image format:format options:options]; diff --git a/SDWebImage/SDImageLoadersManager.h b/SDWebImage/SDImageLoadersManager.h index 651b5e9e..948ca3fa 100644 --- a/SDWebImage/SDImageLoadersManager.h +++ b/SDWebImage/SDImageLoadersManager.h @@ -18,7 +18,7 @@ /** All image loaders in manager. The loaders array is a priority queue, which means the later added loader will have the highest priority */ -@property (nonatomic, copy, readwrite, nullable) NSArray>* loaders; +@property (nonatomic, copy, nullable) NSArray>* loaders; /** Add a new image loader to the end of loaders array. Which has the highest priority. diff --git a/SDWebImage/SDImageLoadersManager.m b/SDWebImage/SDImageLoadersManager.m index ecb476c5..91e0d941 100644 --- a/SDWebImage/SDImageLoadersManager.m +++ b/SDWebImage/SDImageLoadersManager.m @@ -16,6 +16,9 @@ @end @implementation SDImageLoadersManager +{ + NSMutableArray>* _imageLoaders; +} + (SDImageLoadersManager *)sharedManager { static dispatch_once_t onceToken; @@ -30,12 +33,28 @@ self = [super init]; if (self) { // initialize with default image loaders - _loaders = @[[SDWebImageDownloader sharedDownloader]]; + _imageLoaders = [NSMutableArray arrayWithObject:[SDWebImageDownloader sharedDownloader]]; _loadersLock = dispatch_semaphore_create(1); } return self; } +- (NSArray> *)loaders { + SD_LOCK(self.loadersLock); + NSArray>* loaders = [_imageLoaders copy]; + SD_UNLOCK(self.loadersLock); + return loaders; +} + +- (void)setLoaders:(NSArray> *)loaders { + SD_LOCK(self.loadersLock); + [_imageLoaders removeAllObjects]; + if (loaders.count) { + [_imageLoaders addObjectsFromArray:loaders]; + } + SD_UNLOCK(self.loadersLock); +} + #pragma mark - Loader Property - (void)addLoader:(id)loader { @@ -43,12 +62,7 @@ return; } SD_LOCK(self.loadersLock); - NSMutableArray> *mutableLoaders = [self.loaders mutableCopy]; - if (!mutableLoaders) { - mutableLoaders = [NSMutableArray array]; - } - [mutableLoaders addObject:loader]; - self.loaders = [mutableLoaders copy]; + [_imageLoaders addObject:loader]; SD_UNLOCK(self.loadersLock); } @@ -57,18 +71,14 @@ return; } SD_LOCK(self.loadersLock); - NSMutableArray> *mutableLoaders = [self.loaders mutableCopy]; - [mutableLoaders removeObject:loader]; - self.loaders = [mutableLoaders copy]; + [_imageLoaders removeObject:loader]; SD_UNLOCK(self.loadersLock); } #pragma mark - SDImageLoader - (BOOL)canRequestImageForURL:(nullable NSURL *)url { - SD_LOCK(self.loadersLock); NSArray> *loaders = self.loaders; - SD_UNLOCK(self.loadersLock); for (id loader in loaders.reverseObjectEnumerator) { if ([loader canRequestImageForURL:url]) { return YES; @@ -81,9 +91,7 @@ if (!url) { return nil; } - SD_LOCK(self.loadersLock); NSArray> *loaders = self.loaders; - SD_UNLOCK(self.loadersLock); for (id loader in loaders.reverseObjectEnumerator) { if ([loader canRequestImageForURL:url]) { return [loader requestImageWithURL:url options:options context:context progress:progressBlock completed:completedBlock];