Merge branch 'refactor_loader_protocol' into 5.x
This commit is contained in:
commit
3c478cbdf2
|
@ -341,24 +341,9 @@ didReceiveResponse:(NSURLResponse *)response
|
|||
// Get the finish status
|
||||
BOOL finished = (totalSize >= self.expectedSize);
|
||||
|
||||
if (!self.progressiveCoder) {
|
||||
// We need to create a new instance for progressive decoding to avoid conflicts
|
||||
for (id<SDWebImageCoder>coder in [SDWebImageCodersManager sharedManager].coders) {
|
||||
if ([coder conformsToProtocol:@protocol(SDWebImageProgressiveCoder)] &&
|
||||
[((id<SDWebImageProgressiveCoder>)coder) canIncrementalDecodeFromData:imageData]) {
|
||||
self.progressiveCoder = [[[coder class] alloc] initIncrementalWithOptions:nil];
|
||||
break;
|
||||
}
|
||||
}
|
||||
// If we can't find any progressive coder, disable progressive download
|
||||
if (!self.progressiveCoder) {
|
||||
self.options &= ~SDWebImageDownloaderProgressiveDownload;
|
||||
}
|
||||
}
|
||||
|
||||
// progressive decode the image in coder queue
|
||||
dispatch_async(self.coderQueue, ^{
|
||||
UIImage *image = SDWebImageLoaderDecodeProgressiveImageData(data, self.request.URL, finished, self.progressiveCoder, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context);
|
||||
UIImage *image = SDWebImageLoaderDecodeProgressiveImageData(data, self.request.URL, finished, self, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context);
|
||||
if (image) {
|
||||
// We do not keep the progressive decoding image even when `finished`=YES. Because they are for view rendering but not take full function from downloader options. And some coders implementation may not keep consistent between progressive decoding and normal decoding.
|
||||
|
||||
|
|
|
@ -14,11 +14,12 @@
|
|||
typedef void(^SDWebImageLoaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL);
|
||||
typedef void(^SDWebImageLoaderCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished);
|
||||
|
||||
#pragma mark - Context
|
||||
#pragma mark - Context Options
|
||||
|
||||
/**
|
||||
A `UIImage` instance from `SDWebImageManager` when you specify `SDWebImageRefreshCached` and image cache hit.
|
||||
This can be a hint for image loader to load the image from network and refresh the image from remote location if needed. If the cached image is equal to the remote location one. you should call the completion with all nil args. (UIImage)
|
||||
This can be a hint for image loader to load the image from network and refresh the image from remote location if needed. If the image from remote location does not change, you should call the completion with `SDWebImageErrorCacheNotModified` error. (UIImage)
|
||||
@note If you don't implement `SDWebImageRefreshCached` support, you do not need to care abot this context option.
|
||||
*/
|
||||
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextLoaderCachedImage;
|
||||
|
||||
|
@ -39,16 +40,15 @@ FOUNDATION_EXPORT UIImage * _Nullable SDWebImageLoaderDecodeImageData(NSData * _
|
|||
/**
|
||||
This is the built-in decoding process for image progressive download from network. It's used when `SDWebImageProgressiveDownload` option is set. (It's not required when your loader does not support progressive image loading)
|
||||
@note If you want to implement your custom loader with `loadImageWithURL:options:context:progress:completed:` API, but also want to keep compatible with SDWebImage's behavior, you'd better use this to produce image.
|
||||
|
||||
@param imageData The image data from the network so far. Should not be nil
|
||||
@param imageURL The image URL from the input. Should not be nil
|
||||
@param finished Pass NO to specify the download process has not finished. Pass YES when all image data has finished.
|
||||
@param progressiveCoder The image progressive coder. Should not be nil. You should bind the progressive coder for each of loading operation to avoid conflict. See `SDWebImageProgressiveCoder`.
|
||||
@param operation The loader operation associated with current progressive download. Why to provide this is because progressive decoding need to store the partial decoded context for each operation to avoid conflict. You should provide the operation from `loadImageWithURL:` method return value.
|
||||
@param options The options arg from the input
|
||||
@param context The context arg from the input
|
||||
@return The decoded progressive image for current image data load from the network
|
||||
*/
|
||||
FOUNDATION_EXPORT UIImage * _Nullable SDWebImageLoaderDecodeProgressiveImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, BOOL finished, id<SDWebImageProgressiveCoder> _Nonnull progressiveCoder, SDWebImageOptions options, SDWebImageContext * _Nullable context);
|
||||
FOUNDATION_EXPORT UIImage * _Nullable SDWebImageLoaderDecodeProgressiveImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, BOOL finished, id<SDWebImageOperation> _Nonnull operation, SDWebImageOptions options, SDWebImageContext * _Nullable context);
|
||||
|
||||
#pragma mark - SDWebImageLoader
|
||||
|
||||
|
|
|
@ -12,6 +12,9 @@
|
|||
#import "SDWebImageCoderHelper.h"
|
||||
#import "SDAnimatedImage.h"
|
||||
#import "UIImage+WebCache.h"
|
||||
#import "objc/runtime.h"
|
||||
|
||||
static void * SDWebImageLoaderProgressiveCoderKey = &SDWebImageLoaderProgressiveCoderKey;
|
||||
|
||||
UIImage * _Nullable SDWebImageLoaderDecodeImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, SDWebImageOptions options, SDWebImageContext * _Nullable context) {
|
||||
NSCParameterAssert(imageData);
|
||||
|
@ -69,10 +72,27 @@ UIImage * _Nullable SDWebImageLoaderDecodeImageData(NSData * _Nonnull imageData,
|
|||
return image;
|
||||
}
|
||||
|
||||
UIImage * _Nullable SDWebImageLoaderDecodeProgressiveImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, BOOL finished, id<SDWebImageProgressiveCoder> _Nonnull progressiveCoder, SDWebImageOptions options, SDWebImageContext * _Nullable context) {
|
||||
UIImage * _Nullable SDWebImageLoaderDecodeProgressiveImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, BOOL finished, id<SDWebImageOperation> _Nonnull operation, SDWebImageOptions options, SDWebImageContext * _Nullable context) {
|
||||
NSCParameterAssert(imageData);
|
||||
NSCParameterAssert(imageURL);
|
||||
NSCParameterAssert(progressiveCoder);
|
||||
NSCParameterAssert(operation);
|
||||
|
||||
id<SDWebImageProgressiveCoder> progressiveCoder = objc_getAssociatedObject(operation, SDWebImageLoaderProgressiveCoderKey);
|
||||
if (!progressiveCoder) {
|
||||
// We need to create a new instance for progressive decoding to avoid conflicts
|
||||
for (id<SDWebImageCoder>coder in [SDWebImageCodersManager sharedManager].coders) {
|
||||
if ([coder conformsToProtocol:@protocol(SDWebImageProgressiveCoder)] &&
|
||||
[((id<SDWebImageProgressiveCoder>)coder) canIncrementalDecodeFromData:imageData]) {
|
||||
progressiveCoder = [[[coder class] alloc] initIncrementalWithOptions:nil];
|
||||
break;
|
||||
}
|
||||
}
|
||||
objc_setAssociatedObject(operation, SDWebImageLoaderProgressiveCoderKey, progressiveCoder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
// If we can't find any progressive coder, disable progressive download
|
||||
if (!progressiveCoder) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
UIImage *image;
|
||||
id<SDWebImageCacheKeyFilter> cacheKeyFilter = [context valueForKey:SDWebImageContextCacheKeyFilter];
|
||||
|
|
Loading…
Reference in New Issue