Move the context arg after the `options` arg to make Swift ABI clear. Also update all view category to support context arg.
This commit is contained in:
parent
d751b20652
commit
8236dee202
|
@ -53,17 +53,29 @@
|
|||
[self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setImageWithURL:url placeholderImage:placeholder options:options context:nil progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
dispatch_group_t group = dispatch_group_create();
|
||||
SDWebImageMutableContext *mutableContext;
|
||||
if (context) {
|
||||
mutableContext = [context mutableCopy];
|
||||
} else {
|
||||
mutableContext = [NSMutableDictionary dictionary];
|
||||
}
|
||||
mutableContext[SDWebImageContextSetImageGroup] = group;
|
||||
__weak typeof(self)weakSelf = self;
|
||||
[self sd_internalSetImageWithURL:url
|
||||
placeholderImage:placeholder
|
||||
options:options
|
||||
operationKey:nil
|
||||
context:mutableContext
|
||||
setImageBlock:^(UIImage *image, NSData *imageData) {
|
||||
// We could not directlly create the animated image on bacakground queue because it's time consuming, by the time we set it back, the current runloop has passed and the placeholder has been rendered and then replaced with animated image, this cause a flashing.
|
||||
// Previously we use a trick to firstly set the static poster image, then set animated image back to avoid flashing, but this trick fail when using with custom UIView transition. Core Animation will use the current layer state to do rendering, so even we later set it back, the transition will not update. (it's recommended to use `SDWebImageTransition` instead)
|
||||
|
@ -103,8 +115,11 @@
|
|||
}
|
||||
}
|
||||
progress:progressBlock
|
||||
completed:completedBlock
|
||||
context:group ? @{SDWebImageContextSetImageGroup : group} : nil];
|
||||
completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
if (completedBlock) {
|
||||
completedBlock(image, error, cacheType, imageURL);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -104,6 +104,52 @@
|
|||
options:(SDWebImageOptions)options
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Set the imageView `image` with an `url`, placeholder and custom options.
|
||||
*
|
||||
* The download is asynchronous and cached.
|
||||
*
|
||||
* @param url The url for the image.
|
||||
* @param placeholder The image to be set initially, until the image request finishes.
|
||||
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the image parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the image was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original image url.
|
||||
*/
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Set the imageView `image` with an `url`, placeholder and custom options.
|
||||
*
|
||||
* The download is asynchronous and cached.
|
||||
*
|
||||
* @param url The url for the image.
|
||||
* @param placeholder The image to be set initially, until the image request finishes.
|
||||
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the image parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the image was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original image url.
|
||||
*/
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
|
|
|
@ -36,20 +36,34 @@
|
|||
[self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setImageWithURL:url placeholderImage:placeholder options:options context:nil progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
__weak typeof(self)weakSelf = self;
|
||||
[self sd_internalSetImageWithURL:url
|
||||
placeholderImage:placeholder
|
||||
options:options
|
||||
operationKey:nil
|
||||
context:context
|
||||
setImageBlock:^(UIImage *image, NSData *imageData) {
|
||||
weakSelf.image = image;
|
||||
}
|
||||
progress:nil
|
||||
completed:completedBlock];
|
||||
progress:progressBlock
|
||||
completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
if (completedBlock) {
|
||||
completedBlock(image, error, cacheType, imageURL);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -128,6 +128,30 @@
|
|||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Set the button `image` with an `url`, placeholder and custom options.
|
||||
*
|
||||
* The download is asynchronous and cached.
|
||||
*
|
||||
* @param url The url for the image.
|
||||
* @param placeholder The image to be set initially, until the image request finishes.
|
||||
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the image parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the image was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original image url.
|
||||
*/
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
#pragma mark - Alternate Image
|
||||
|
||||
/**
|
||||
|
@ -242,6 +266,30 @@
|
|||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Set the button `alternateImage` with an `url`, placeholder and custom options.
|
||||
*
|
||||
* The download is asynchronous and cached.
|
||||
*
|
||||
* @param url The url for the alternateImage.
|
||||
* @param placeholder The alternateImage to be set initially, until the alternateImage request finishes.
|
||||
* @param options The options to use when downloading the alternateImage. @see SDWebImageOptions for the possible values.
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
* @param progressBlock A block called while alternateImage is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the alternateImage parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the alternateImage was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original alternateImage url.
|
||||
*/
|
||||
- (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
#pragma mark - Cancel
|
||||
|
||||
/**
|
||||
|
|
|
@ -14,13 +14,7 @@
|
|||
#import "UIView+WebCacheOperation.h"
|
||||
#import "UIView+WebCache.h"
|
||||
|
||||
static inline NSString * imageOperationKey() {
|
||||
return @"NSButtonImageOperation";
|
||||
}
|
||||
|
||||
static inline NSString * alternateImageOperationKey() {
|
||||
return @"NSButtonAlternateImageOperation";
|
||||
}
|
||||
static NSString * const SDAlternateImageOperationKey = @"NSButtonAlternateImageOperation";
|
||||
|
||||
@implementation NSButton (WebCache)
|
||||
|
||||
|
@ -50,23 +44,28 @@ static inline NSString * alternateImageOperationKey() {
|
|||
[self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setImageWithURL:url placeholderImage:placeholder options:options context:nil progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
self.sd_currentImageURL = url;
|
||||
|
||||
__weak typeof(self)weakSelf = self;
|
||||
[self sd_internalSetImageWithURL:url
|
||||
placeholderImage:placeholder
|
||||
options:options
|
||||
operationKey:imageOperationKey()
|
||||
setImageBlock:^(NSImage * _Nullable image, NSData * _Nullable imageData) {
|
||||
weakSelf.image = image;
|
||||
}
|
||||
context:context
|
||||
setImageBlock:nil
|
||||
progress:progressBlock
|
||||
completed:completedBlock];
|
||||
completed:^(NSImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
if (completedBlock) {
|
||||
completedBlock(image, error, cacheType, imageURL);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
#pragma mark - Alternate Image
|
||||
|
@ -95,33 +94,49 @@ static inline NSString * alternateImageOperationKey() {
|
|||
[self sd_setAlternateImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setAlternateImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setAlternateImageWithURL:url placeholderImage:placeholder options:options context:nil progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
self.sd_currentAlternateImageURL = url;
|
||||
|
||||
SDWebImageMutableContext *mutableContext;
|
||||
if (context) {
|
||||
mutableContext = [context mutableCopy];
|
||||
} else {
|
||||
mutableContext = [NSMutableDictionary dictionary];
|
||||
}
|
||||
mutableContext[SDWebImageContextSetImageOperationKey] = SDAlternateImageOperationKey;
|
||||
__weak typeof(self)weakSelf = self;
|
||||
[self sd_internalSetImageWithURL:url
|
||||
placeholderImage:placeholder
|
||||
options:options
|
||||
operationKey:alternateImageOperationKey()
|
||||
context:mutableContext
|
||||
setImageBlock:^(NSImage * _Nullable image, NSData * _Nullable imageData) {
|
||||
weakSelf.alternateImage = image;
|
||||
}
|
||||
progress:progressBlock
|
||||
completed:completedBlock];
|
||||
completed:^(NSImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
if (completedBlock) {
|
||||
completedBlock(image, error, cacheType, imageURL);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
#pragma mark - Cancel
|
||||
|
||||
- (void)sd_cancelCurrentImageLoad {
|
||||
[self sd_cancelImageLoadOperationWithKey:imageOperationKey()];
|
||||
[self sd_cancelImageLoadOperationWithKey:NSStringFromClass([self class])];
|
||||
}
|
||||
|
||||
- (void)sd_cancelCurrentAlternateImageLoad {
|
||||
[self sd_cancelImageLoadOperationWithKey:alternateImageOperationKey()];
|
||||
[self sd_cancelImageLoadOperationWithKey:SDAlternateImageOperationKey];
|
||||
}
|
||||
|
||||
#pragma mar - Private
|
||||
|
|
|
@ -121,6 +121,30 @@
|
|||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Set the imageView `image` with an `url`, placeholder and custom options.
|
||||
*
|
||||
* The download is asynchronous and cached.
|
||||
*
|
||||
* @param url The url for the image.
|
||||
* @param placeholder The image to be set initially, until the image request finishes.
|
||||
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the image parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the image was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original image url.
|
||||
*/
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
|
|
|
@ -39,21 +39,35 @@
|
|||
[self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setImageWithURL:url placeholderImage:placeholder options:options context:nil progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
Class animatedImageClass = [SDAnimatedImage class];
|
||||
SDWebImageContext *context = @{SDWebImageContextAnimatedImageClass : animatedImageClass};
|
||||
SDWebImageMutableContext *mutableContext;
|
||||
if (context) {
|
||||
mutableContext = [context mutableCopy];
|
||||
} else {
|
||||
mutableContext = [NSMutableDictionary dictionary];
|
||||
}
|
||||
mutableContext[SDWebImageContextAnimatedImageClass] = animatedImageClass;
|
||||
[self sd_internalSetImageWithURL:url
|
||||
placeholderImage:placeholder
|
||||
options:options
|
||||
operationKey:nil
|
||||
context:mutableContext
|
||||
setImageBlock:nil
|
||||
progress:progressBlock
|
||||
completed:completedBlock
|
||||
context:context];
|
||||
completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
if (completedBlock) {
|
||||
completedBlock(image, error, cacheType, imageURL);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -216,12 +216,12 @@ typedef void(^SDWebImageCompletionWithPossibleErrorBlock)(NSError * _Nullable er
|
|||
*
|
||||
* @param key The unique key used to store the wanted image
|
||||
* @param options A mask to specify options to use for this cache query
|
||||
* @param doneBlock The completion block. Will not get called if the operation is cancelled
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
* @param doneBlock The completion block. Will not get called if the operation is cancelled
|
||||
*
|
||||
* @return a NSOperation instance containing the cache op
|
||||
*/
|
||||
- (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options done:(nullable SDCacheQueryCompletedBlock)doneBlock context:(nullable SDWebImageContext *)context;
|
||||
- (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options context:(nullable SDWebImageContext *)context done:(nullable SDCacheQueryCompletedBlock)doneBlock;
|
||||
|
||||
/**
|
||||
* Synchronously query the memory cache.
|
||||
|
|
|
@ -530,10 +530,10 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
|
|||
}
|
||||
|
||||
- (nullable NSOperation *)queryCacheOperationForKey:(NSString *)key options:(SDImageCacheOptions)options done:(SDCacheQueryCompletedBlock)doneBlock {
|
||||
return [self queryCacheOperationForKey:key options:options done:doneBlock context:nil];
|
||||
return [self queryCacheOperationForKey:key options:options context:nil done:doneBlock];
|
||||
}
|
||||
|
||||
- (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options done:(nullable SDCacheQueryCompletedBlock)doneBlock context:(nullable SDWebImageContext *)context {
|
||||
- (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options context:(nullable SDWebImageContext *)context done:(nullable SDCacheQueryCompletedBlock)doneBlock {
|
||||
if (!key) {
|
||||
if (doneBlock) {
|
||||
doneBlock(nil, nil, SDImageCacheTypeNone);
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
typedef void(^SDWebImageNoParamsBlock)(void);
|
||||
typedef NSString * SDWebImageContextOption NS_STRING_ENUM;
|
||||
typedef NSDictionary<SDWebImageContextOption, id> SDWebImageContext;
|
||||
typedef NSMutableDictionary<SDWebImageContextOption, id> SDWebImageMutableContext;
|
||||
|
||||
#pragma mark - Image scale
|
||||
|
||||
|
@ -156,12 +157,17 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
|
|||
#pragma mark - Context Options
|
||||
|
||||
/**
|
||||
A Dispatch group to maintain setImageBlock and completionBlock. This is used for custom setImageBlock advanced usage, such like perform background task but need to guarantee the completion block is called after setImageBlock. (dispatch_group_t)
|
||||
A String to be used as the operation key for view category to store the image load operation. This is used for view instance which supports different image loading process. If nil, will use the class name as operation key. (NSString *)
|
||||
*/
|
||||
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSetImageOperationKey;
|
||||
|
||||
/**
|
||||
A Dispatch group to maintain setImageBlock and completionBlock for view category. This is used for custom setImageBlock advanced usage, such like perform background task but need to guarantee the completion block is called after setImageBlock. (dispatch_group_t)
|
||||
*/
|
||||
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSetImageGroup;
|
||||
|
||||
/**
|
||||
A SDWebImageManager instance to control the image download and cache process using in UIImageView+WebCache category and likes. If not provided, use the shared manager (SDWebImageManager)
|
||||
A SDWebImageManager instance to control the image download and cache process using in UIImageView+WebCache category and likes. If not provided, use the shared manager (SDWebImageManager *)
|
||||
*/
|
||||
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextCustomManager;
|
||||
|
||||
|
|
|
@ -99,6 +99,7 @@ inline UIImage *SDScaledImageForKey(NSString * _Nullable key, UIImage * _Nullabl
|
|||
|
||||
#pragma mark - Context option
|
||||
|
||||
SDWebImageContextOption const SDWebImageContextSetImageOperationKey = @"setImageOperationKey";
|
||||
SDWebImageContextOption const SDWebImageContextSetImageGroup = @"setImageGroup";
|
||||
SDWebImageContextOption const SDWebImageContextCustomManager = @"customManager";
|
||||
SDWebImageContextOption const SDWebImageContextCustomTransformer = @"customTransformer";
|
||||
|
|
|
@ -254,18 +254,18 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB
|
|||
*
|
||||
* @param url The URL to the image to download
|
||||
* @param options The options to be used for this download
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
* @param progressBlock A block called repeatedly while the image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called once the download is completed.
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
*
|
||||
* @return A token (SDWebImageDownloadToken) that can be passed to -cancel: to cancel this operation
|
||||
*/
|
||||
- (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
|
||||
options:(SDWebImageDownloaderOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock
|
||||
context:(nullable SDWebImageContext *)context;
|
||||
completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Cancels a download that was previously queued using -downloadImageWithURL:options:progress:completed:
|
||||
|
|
|
@ -193,14 +193,14 @@
|
|||
}
|
||||
|
||||
- (nullable SDWebImageDownloadToken *)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
|
||||
return [self downloadImageWithURL:url options:options progress:progressBlock completed:completedBlock context:nil];
|
||||
return [self downloadImageWithURL:url options:options context:nil progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
- (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
|
||||
options:(SDWebImageDownloaderOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock
|
||||
context:(nullable SDWebImageContext *)context {
|
||||
completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock {
|
||||
__weak SDWebImageDownloader *wself = self;
|
||||
|
||||
return [self addProgressCallback:progressBlock completedBlock:completedBlock forURL:url createCallback:^SDWebImageDownloaderOperation *{
|
||||
|
|
|
@ -180,18 +180,18 @@ SDWebImageManager.sharedManager.cacheKeyFilter = ^(NSURL * _Nullable url) {
|
|||
*
|
||||
* @param url The URL to the image
|
||||
* @param options A mask to specify options to use for this request
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed.
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
*
|
||||
* @return Returns an NSObject conforming to SDWebImageOperation. Should be an instance of SDWebImageDownloaderOperation
|
||||
*/
|
||||
- (nullable id <SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nonnull SDInternalCompletionBlock)completedBlock
|
||||
context:(nullable SDWebImageContext *)context;
|
||||
completed:(nonnull SDInternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Saves image to cache for given URL
|
||||
|
|
|
@ -109,14 +109,14 @@
|
|||
}
|
||||
|
||||
- (id<SDWebImageOperation>)loadImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDInternalCompletionBlock)completedBlock {
|
||||
return [self loadImageWithURL:url options:options progress:progressBlock completed:completedBlock context:nil];
|
||||
return [self loadImageWithURL:url options:options context:nil progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
- (id<SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nonnull SDInternalCompletionBlock)completedBlock
|
||||
context:(nullable SDWebImageContext *)context {
|
||||
completed:(nonnull SDInternalCompletionBlock)completedBlock {
|
||||
// Invoking this method without a completedBlock is pointless
|
||||
NSAssert(completedBlock != nil, @"If you mean to prefetch the image, use -[SDWebImagePrefetcher prefetchURLs] instead");
|
||||
|
||||
|
@ -165,13 +165,18 @@
|
|||
} else if (self.transformer) {
|
||||
// Transformer from manager
|
||||
transformer = self.transformer;
|
||||
NSMutableDictionary<SDWebImageContextOption, id> *mutableContext = [NSMutableDictionary dictionaryWithDictionary:context];
|
||||
SDWebImageMutableContext *mutableContext;
|
||||
if (context) {
|
||||
mutableContext = [context mutableCopy];
|
||||
} else {
|
||||
mutableContext = [NSMutableDictionary dictionary];
|
||||
}
|
||||
[mutableContext setValue:transformer forKey:SDWebImageContextCustomTransformer];
|
||||
context = [mutableContext copy];
|
||||
}
|
||||
|
||||
__weak SDWebImageCombinedOperation *weakOperation = operation;
|
||||
operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key options:cacheOptions done:^(UIImage *cachedImage, NSData *cachedData, SDImageCacheType cacheType) {
|
||||
operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key options:cacheOptions context:context done:^(UIImage *cachedImage, NSData *cachedData, SDImageCacheType cacheType) {
|
||||
__strong __typeof(weakOperation) strongOperation = weakOperation;
|
||||
if (!strongOperation || strongOperation.isCancelled) {
|
||||
[self safelyRemoveOperationFromRunning:strongOperation];
|
||||
|
@ -211,7 +216,7 @@
|
|||
|
||||
// `SDWebImageCombinedOperation` -> `SDWebImageDownloadToken` -> `downloadOperationCancelToken`, which is a `SDCallbacksDictionary` and retain the completed block below, so we need weak-strong again to avoid retain cycle
|
||||
__weak typeof(strongOperation) weakSubOperation = strongOperation;
|
||||
strongOperation.downloadToken = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *downloadedData, NSError *error, BOOL finished) {
|
||||
strongOperation.downloadToken = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions context:context progress:progressBlock completed:^(UIImage *downloadedImage, NSData *downloadedData, NSError *error, BOOL finished) {
|
||||
__strong typeof(weakSubOperation) strongSubOperation = weakSubOperation;
|
||||
if (!strongSubOperation || strongSubOperation.isCancelled) {
|
||||
// Do nothing if the operation was cancelled
|
||||
|
@ -293,7 +298,7 @@
|
|||
if (finished) {
|
||||
[self safelyRemoveOperationFromRunning:strongSubOperation];
|
||||
}
|
||||
} context:context];
|
||||
}];
|
||||
} else if (cachedImage) {
|
||||
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:cachedImage data:cachedData error:nil cacheType:cacheType finished:YES url:url];
|
||||
[self safelyRemoveOperationFromRunning:strongOperation];
|
||||
|
@ -302,7 +307,7 @@
|
|||
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:nil data:nil error:nil cacheType:SDImageCacheTypeNone finished:YES url:url];
|
||||
[self safelyRemoveOperationFromRunning:strongOperation];
|
||||
}
|
||||
} context:context];
|
||||
}];
|
||||
|
||||
return operation;
|
||||
}
|
||||
|
|
|
@ -86,7 +86,7 @@
|
|||
NSPointerArray *operations = token.operations;
|
||||
for (NSURL *url in urls) {
|
||||
__weak typeof(self) wself = self;
|
||||
id<SDWebImageOperation> operation = [self.manager loadImageWithURL:url options:self.options progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
id<SDWebImageOperation> operation = [self.manager loadImageWithURL:url options:self.options context:self.context progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
__strong typeof(wself) sself = wself;
|
||||
if (!sself) {
|
||||
return;
|
||||
|
@ -108,7 +108,7 @@
|
|||
[sself callCompletionBlockForToken:token];
|
||||
[sself removeRunningToken:token];
|
||||
}
|
||||
} context:self.context];
|
||||
}];
|
||||
@synchronized (token) {
|
||||
[operations addPointer:(__bridge void *)operation];
|
||||
}
|
||||
|
|
|
@ -128,6 +128,56 @@
|
|||
options:(SDWebImageOptions)options
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Set the imageView `image` with an `url`, placeholder and custom options.
|
||||
*
|
||||
* The download is asynchronous and cached.
|
||||
*
|
||||
* @param url The url for the image.
|
||||
* @param state The state that uses the specified title. The values are described in UIControlState.
|
||||
* @param placeholder The image to be set initially, until the image request finishes.
|
||||
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the image parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the image was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original image url.
|
||||
*/
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
forState:(UIControlState)state
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Set the imageView `image` with an `url`, placeholder and custom options.
|
||||
*
|
||||
* The download is asynchronous and cached.
|
||||
*
|
||||
* @param url The url for the image.
|
||||
* @param state The state that uses the specified title. The values are described in UIControlState.
|
||||
* @param placeholder The image to be set initially, until the image request finishes.
|
||||
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the image parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the image was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original image url.
|
||||
*/
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
forState:(UIControlState)state
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
#pragma mark - Background Image
|
||||
|
||||
/**
|
||||
|
@ -238,6 +288,54 @@
|
|||
options:(SDWebImageOptions)options
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Set the backgroundImageView `image` with an `url`, placeholder and custom options.
|
||||
*
|
||||
* The download is asynchronous and cached.
|
||||
*
|
||||
* @param url The url for the image.
|
||||
* @param placeholder The image to be set initially, until the image request finishes.
|
||||
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the image parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the image was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original image url.
|
||||
*/
|
||||
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
|
||||
forState:(UIControlState)state
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Set the backgroundImageView `image` with an `url`, placeholder and custom options.
|
||||
*
|
||||
* The download is asynchronous and cached.
|
||||
*
|
||||
* @param url The url for the image.
|
||||
* @param placeholder The image to be set initially, until the image request finishes.
|
||||
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the image parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the image was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original image url.
|
||||
*/
|
||||
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
|
||||
forState:(UIControlState)state
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
#pragma mark - Cancel
|
||||
|
||||
/**
|
||||
|
|
|
@ -72,10 +72,20 @@ static inline NSString * backgroundImageOperationKeyForState(UIControlState stat
|
|||
[self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:options progress:nil completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:options context:nil progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
forState:(UIControlState)state
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
if (!url) {
|
||||
[self.sd_imageURLStorage removeObjectForKey:imageURLKeyForState(state)];
|
||||
|
@ -83,16 +93,27 @@ static inline NSString * backgroundImageOperationKeyForState(UIControlState stat
|
|||
self.sd_imageURLStorage[imageURLKeyForState(state)] = url;
|
||||
}
|
||||
|
||||
SDWebImageMutableContext *mutableContext;
|
||||
if (context) {
|
||||
mutableContext = [context mutableCopy];
|
||||
} else {
|
||||
mutableContext = [NSMutableDictionary dictionary];
|
||||
}
|
||||
mutableContext[SDWebImageContextSetImageOperationKey] = imageOperationKeyForState(state);
|
||||
__weak typeof(self)weakSelf = self;
|
||||
[self sd_internalSetImageWithURL:url
|
||||
placeholderImage:placeholder
|
||||
options:options
|
||||
operationKey:imageOperationKeyForState(state)
|
||||
context:mutableContext
|
||||
setImageBlock:^(UIImage *image, NSData *imageData) {
|
||||
[weakSelf setImage:image forState:state];
|
||||
}
|
||||
progress:nil
|
||||
completed:completedBlock];
|
||||
progress:progressBlock
|
||||
completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
if (completedBlock) {
|
||||
completedBlock(image, error, cacheType, imageURL);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
#pragma mark - Background Image
|
||||
|
@ -131,10 +152,20 @@ static inline NSString * backgroundImageOperationKeyForState(UIControlState stat
|
|||
[self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 context:nil progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
|
||||
forState:(UIControlState)state
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
if (!url) {
|
||||
[self.sd_imageURLStorage removeObjectForKey:backgroundImageURLKeyForState(state)];
|
||||
|
@ -142,16 +173,27 @@ static inline NSString * backgroundImageOperationKeyForState(UIControlState stat
|
|||
self.sd_imageURLStorage[backgroundImageURLKeyForState(state)] = url;
|
||||
}
|
||||
|
||||
SDWebImageMutableContext *mutableContext;
|
||||
if (context) {
|
||||
mutableContext = [context mutableCopy];
|
||||
} else {
|
||||
mutableContext = [NSMutableDictionary dictionary];
|
||||
}
|
||||
mutableContext[SDWebImageContextSetImageOperationKey] = imageOperationKeyForState(state);
|
||||
__weak typeof(self)weakSelf = self;
|
||||
[self sd_internalSetImageWithURL:url
|
||||
placeholderImage:placeholder
|
||||
options:options
|
||||
operationKey:backgroundImageOperationKeyForState(state)
|
||||
context:mutableContext
|
||||
setImageBlock:^(UIImage *image, NSData *imageData) {
|
||||
[weakSelf setBackgroundImage:image forState:state];
|
||||
}
|
||||
progress:nil
|
||||
completed:completedBlock];
|
||||
progress:progressBlock
|
||||
completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
if (completedBlock) {
|
||||
completedBlock(image, error, cacheType, imageURL);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
#pragma mark - Cancel
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
#import "UIView+WebCacheOperation.h"
|
||||
#import "UIView+WebCache.h"
|
||||
|
||||
static NSString * const SDHighlightedImageOperationKey = @"UIImageViewImageOperationHighlighted";
|
||||
|
||||
@implementation UIImageView (HighlightedWebCache)
|
||||
|
||||
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url {
|
||||
|
@ -31,20 +33,36 @@
|
|||
[self sd_setHighlightedImageWithURL:url options:options progress:nil completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setHighlightedImageWithURL:url options:options context:nil progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setHighlightedImageWithURL:(nullable NSURL *)url
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
__weak typeof(self)weakSelf = self;
|
||||
SDWebImageMutableContext *mutableContext;
|
||||
if (context) {
|
||||
mutableContext = [context mutableCopy];
|
||||
} else {
|
||||
mutableContext = [NSMutableDictionary dictionary];
|
||||
}
|
||||
mutableContext[SDWebImageContextSetImageOperationKey] = SDHighlightedImageOperationKey;
|
||||
[self sd_internalSetImageWithURL:url
|
||||
placeholderImage:nil
|
||||
options:options
|
||||
operationKey:@"UIImageViewImageOperationHighlighted"
|
||||
context:mutableContext
|
||||
setImageBlock:^(UIImage *image, NSData *imageData) {
|
||||
weakSelf.highlightedImage = image;
|
||||
}
|
||||
progress:progressBlock
|
||||
completed:completedBlock];
|
||||
completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
if (completedBlock) {
|
||||
completedBlock(image, error, cacheType, imageURL);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -154,6 +154,30 @@
|
|||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Set the imageView `image` with an `url`, placeholder and custom options.
|
||||
*
|
||||
* The download is asynchronous and cached.
|
||||
*
|
||||
* @param url The url for the image.
|
||||
* @param placeholder The image to be set initially, until the image request finishes.
|
||||
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the image parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the image was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original image url.
|
||||
*/
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
#if SD_UIKIT
|
||||
|
||||
#pragma mark - Animation of multiple images
|
||||
|
|
|
@ -40,18 +40,27 @@
|
|||
[self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_setImageWithURL:url placeholderImage:placeholder options:options context:nil progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
- (void)sd_setImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
[self sd_internalSetImageWithURL:url
|
||||
placeholderImage:placeholder
|
||||
options:options
|
||||
operationKey:nil
|
||||
context:context
|
||||
setImageBlock:nil
|
||||
progress:progressBlock
|
||||
completed:completedBlock];
|
||||
completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
if (completedBlock) {
|
||||
completedBlock(image, error, cacheType, imageURL);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
#if SD_UIKIT
|
||||
|
|
|
@ -48,51 +48,29 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima
|
|||
* @param url The url for the image.
|
||||
* @param placeholder The image to be set initially, until the image request finishes.
|
||||
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
|
||||
* @param operationKey A string to be used as the operation key. If nil, will use the class name
|
||||
* @param setImageBlock Block used for custom set image code
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the image parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the image was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original image url.
|
||||
*/
|
||||
- (void)sd_internalSetImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
operationKey:(nullable NSString *)operationKey
|
||||
setImageBlock:(nullable SDSetImageBlock)setImageBlock
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Set the imageView `image` with an `url` and optionally a placeholder image.
|
||||
*
|
||||
* The download is asynchronous and cached.
|
||||
*
|
||||
* @param url The url for the image.
|
||||
* @param placeholder The image to be set initially, until the image request finishes.
|
||||
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
|
||||
* @param operationKey A string to be used as the operation key. If nil, will use the class name
|
||||
* @param setImageBlock Block used for custom set image code
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed. This block has no return value
|
||||
* and takes the requested UIImage as first parameter. In case of error the image parameter
|
||||
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
|
||||
* indicating if the image was retrieved from the local cache or from the network.
|
||||
* The fourth parameter is the original image url.
|
||||
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
* @param setImageBlock Block used for custom set image code. If not provide, use the built-in set image code (supports `UIImageView/NSImageView` and `UIButton/NSButton` currently)
|
||||
* @param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
* @param completedBlock A block called when operation has been completed.
|
||||
* This block has no return value and takes the requested UIImage as first parameter and the NSData representation as second parameter.
|
||||
* In case of error the image parameter is nil and the third parameter may contain an NSError.
|
||||
*
|
||||
* The forth parameter is an `SDImageCacheType` enum indicating if the image was retrieved from the local cache
|
||||
* or from the memory cache or from the network.
|
||||
*
|
||||
* The fith parameter normally is always YES. However, if you provide SDWebImageAvoidAutoSetImage with SDWebImageProgressiveDownload options to enable progressive downloading and set the image yourself. This block is thus called repeatedly with a partial image. When image is fully downloaded, the
|
||||
* block is called a last time with the full image and the last parameter set to YES.
|
||||
*
|
||||
* The last parameter is the original image URL
|
||||
*/
|
||||
- (void)sd_internalSetImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
operationKey:(nullable NSString *)operationKey
|
||||
context:(nullable SDWebImageContext *)context
|
||||
setImageBlock:(nullable SDSetImageBlock)setImageBlock
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock
|
||||
context:(nullable SDWebImageContext *)context;
|
||||
completed:(nullable SDInternalCompletionBlock)completedBlock;
|
||||
|
||||
/**
|
||||
* Cancel the current image load
|
||||
|
|
|
@ -15,16 +15,14 @@
|
|||
|
||||
const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
|
||||
|
||||
static char imageURLKey;
|
||||
|
||||
@implementation UIView (WebCache)
|
||||
|
||||
- (nullable NSURL *)sd_imageURL {
|
||||
return objc_getAssociatedObject(self, &imageURLKey);
|
||||
return objc_getAssociatedObject(self, @selector(sd_imageURL));
|
||||
}
|
||||
|
||||
- (void)setSd_imageURL:(NSURL * _Nullable)sd_imageURL {
|
||||
objc_setAssociatedObject(self, &imageURLKey, sd_imageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
objc_setAssociatedObject(self, @selector(sd_imageURL), sd_imageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
|
||||
- (NSProgress *)sd_imageProgress {
|
||||
|
@ -43,28 +41,31 @@ static char imageURLKey;
|
|||
- (void)sd_internalSetImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
operationKey:(nullable NSString *)operationKey
|
||||
setImageBlock:(nullable SDSetImageBlock)setImageBlock
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock {
|
||||
return [self sd_internalSetImageWithURL:url placeholderImage:placeholder options:options operationKey:operationKey setImageBlock:setImageBlock progress:progressBlock completed:completedBlock context:nil];
|
||||
}
|
||||
|
||||
- (void)sd_internalSetImageWithURL:(nullable NSURL *)url
|
||||
placeholderImage:(nullable UIImage *)placeholder
|
||||
options:(SDWebImageOptions)options
|
||||
operationKey:(nullable NSString *)operationKey
|
||||
setImageBlock:(nullable SDSetImageBlock)setImageBlock
|
||||
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
|
||||
completed:(nullable SDExternalCompletionBlock)completedBlock
|
||||
context:(nullable SDWebImageContext *)context {
|
||||
NSString *validOperationKey = operationKey ?: NSStringFromClass([self class]);
|
||||
context:(nullable SDWebImageContext *)context
|
||||
setImageBlock:(nullable SDSetImageBlock)setImageBlock progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDInternalCompletionBlock)completedBlock {
|
||||
context = [context copy]; // copy to avoid mutable object
|
||||
NSString *validOperationKey = nil;
|
||||
if ([context valueForKey:SDWebImageContextSetImageOperationKey]) {
|
||||
validOperationKey = [context valueForKey:SDWebImageContextSetImageOperationKey];
|
||||
} else {
|
||||
validOperationKey = NSStringFromClass([self class]);
|
||||
}
|
||||
dispatch_group_t group = nil;
|
||||
if ([context valueForKey:SDWebImageContextSetImageGroup]) {
|
||||
group = [context valueForKey:SDWebImageContextSetImageGroup];
|
||||
}
|
||||
if (context && group) {
|
||||
// Remove the context option for View Category only and pass others for manager
|
||||
// Operation key may be useful for some advanced feature, keep it
|
||||
SDWebImageMutableContext *mutableContext = [context mutableCopy];
|
||||
[mutableContext removeObjectForKey:SDWebImageContextSetImageGroup];
|
||||
context = [mutableContext copy];
|
||||
}
|
||||
[self sd_cancelImageLoadOperationWithKey:validOperationKey];
|
||||
self.sd_imageURL = url;
|
||||
|
||||
if (!(options & SDWebImageDelayPlaceholder)) {
|
||||
if ([context valueForKey:SDWebImageContextSetImageGroup]) {
|
||||
dispatch_group_t group = [context valueForKey:SDWebImageContextSetImageGroup];
|
||||
if (group) {
|
||||
dispatch_group_enter(group);
|
||||
}
|
||||
dispatch_main_async_safe(^{
|
||||
|
@ -103,7 +104,7 @@ static char imageURLKey;
|
|||
});
|
||||
}
|
||||
};
|
||||
id <SDWebImageOperation> operation = [manager loadImageWithURL:url options:options progress:combinedProgressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
|
||||
id <SDWebImageOperation> operation = [manager loadImageWithURL:url options:options context:context progress:combinedProgressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
|
||||
__strong __typeof (wself) sself = wself;
|
||||
if (!sself) { return; }
|
||||
// if the progress not been updated, mark it to complete state
|
||||
|
@ -126,7 +127,7 @@ static char imageURLKey;
|
|||
[sself sd_setNeedsLayout];
|
||||
}
|
||||
if (completedBlock && shouldCallCompletedBlock) {
|
||||
completedBlock(image, error, cacheType, url);
|
||||
completedBlock(image, data, error, cacheType, finished, url);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -156,8 +157,7 @@ static char imageURLKey;
|
|||
transition = sself.sd_imageTransition;
|
||||
}
|
||||
|
||||
if ([context valueForKey:SDWebImageContextSetImageGroup]) {
|
||||
dispatch_group_t group = [context valueForKey:SDWebImageContextSetImageGroup];
|
||||
if (group) {
|
||||
dispatch_group_enter(group);
|
||||
dispatch_main_async_safe(^{
|
||||
[sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL];
|
||||
|
@ -172,14 +172,14 @@ static char imageURLKey;
|
|||
callCompletedBlockClojure();
|
||||
});
|
||||
}
|
||||
} context:context];
|
||||
}];
|
||||
[self sd_setImageLoadOperation:operation forKey:validOperationKey];
|
||||
} else {
|
||||
[self sd_stopImageIndicator];
|
||||
dispatch_main_async_safe(^{
|
||||
if (completedBlock) {
|
||||
NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
|
||||
completedBlock(nil, error, SDImageCacheTypeNone, url);
|
||||
completedBlock(nil, nil, error, YES, SDImageCacheTypeNone, url);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -215,6 +215,14 @@ static char imageURLKey;
|
|||
};
|
||||
}
|
||||
#endif
|
||||
#if SD_MAC
|
||||
else if ([view isKindOfClass:[NSButton class]]) {
|
||||
NSButton *button = (NSButton *)view;
|
||||
finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData){
|
||||
button.image = setImage;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
if (transition) {
|
||||
#if SD_UIKIT
|
||||
|
|
|
@ -160,7 +160,7 @@
|
|||
|
||||
// Clear the disk cache to force download from network
|
||||
[[SDImageCache sharedImageCache] removeImageForKey:kTestJpegURL withCompletion:^{
|
||||
[view sd_internalSetImageWithURL:originalImageURL placeholderImage:nil options:0 operationKey:nil setImageBlock:nil progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
|
||||
[view sd_internalSetImageWithURL:originalImageURL placeholderImage:nil options:0 context:nil progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
expect(view.sd_imageProgress.fractionCompleted).equal(1.0);
|
||||
expect([view.sd_imageProgress.userInfo[NSStringFromSelector(_cmd)] boolValue]).equal(YES);
|
||||
[expectation fulfill];
|
||||
|
|
Loading…
Reference in New Issue