Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SDWebImageAvoidDecodeImage to allow user to control force decode feature for individual image request #2283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions SDWebImage/SDImageCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
* Use this flag to transform them anyway.
*/
SDImageCacheTransformAnimatedImage = 1 << 2,
/**
* By default, we will decode the image in the background during cache query and download from the network. This can help to improve performance because when rendering image on the screen, it need to be firstly decoded. But this happen on the main queue by Core Animation.
* However, this process may increase the memory usage as well. If you are experiencing a issue due to excessive memory consumption, This flag can prevent decode the image.
*/
SDImageCacheAvoidDecodeImage = 1 << 3,
/**
* By default, we decode the animated image. This flag can force decode the first frame only and produece the static image.
*/
SDImageCacheDecodeFirstFrameOnly = 1 << 3,
SDImageCacheDecodeFirstFrameOnly = 1 << 4,
/**
* By default, for `SDAnimatedImage`, we decode the animated image frame during rendering to reduce memory usage. This flag actually trigger `preloadAllAnimatedImageFrames = YES` after image load from disk cache
*/
SDImageCachePreloadAllFrames = 1 << 4
SDImageCachePreloadAllFrames = 1 << 5
};

typedef void(^SDCacheQueryCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType);
Expand Down
6 changes: 2 additions & 4 deletions SDWebImage/SDImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ - (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NS
if (!image) {
image = [[SDWebImageCodersManager sharedManager] decodedImageWithData:data options:@{SDWebImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDWebImageCoderDecodeScaleFactor : @(scale)}];
}
BOOL shouldDecode = YES;
BOOL shouldDecode = (options & SDImageCacheAvoidDecodeImage) == 0;
if ([image conformsToProtocol:@protocol(SDAnimatedImage)]) {
// `SDAnimatedImage` do not decode
shouldDecode = NO;
Expand All @@ -482,9 +482,7 @@ - (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NS
shouldDecode = NO;
}
if (shouldDecode) {
if (self.config.shouldDecompressImages) {
image = [SDWebImageCoderHelper decodedImageWithImage:image];
}
image = [SDWebImageCoderHelper decodedImageWithImage:image];
}
return image;
} else {
Expand Down
6 changes: 0 additions & 6 deletions SDWebImage/SDImageCacheConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@

@interface SDImageCacheConfig : NSObject

/**
* Decompressing images means pre-decoding the image that are downloaded and cached on background queue. This can avoid image view decode it on main queue when rendering. This can improve performance but can consume more memory.
* Defaults to YES. Set this to NO if you are experiencing a crash due to excessive memory consumption.
*/
@property (assign, nonatomic) BOOL shouldDecompressImages;

/**
* Whether or not to disable iCloud backup
* Defaults to YES.
Expand Down
1 change: 0 additions & 1 deletion SDWebImage/SDImageCacheConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ @implementation SDImageCacheConfig

- (instancetype)init {
if (self = [super init]) {
_shouldDecompressImages = YES;
_shouldDisableiCloud = YES;
_shouldCacheImagesInMemory = YES;
_diskCacheReadingOptions = 0;
Expand Down
8 changes: 7 additions & 1 deletion SDWebImage/SDWebImageDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
/**
* By default, images are decoded respecting their original size. On iOS, this flag will scale down the
* images to a size compatible with the constrained memory of devices.
* If `SDWebImageProgressiveDownload` flag is set the scale down is deactivated.
* This flag take no effect if `SDWebImageAvoidDecodeImage` is set. And it will be ignored if `SDWebImageProgressiveDownload` is set.
*/
SDWebImageScaleDownLargeImages = 1 << 12,

Expand All @@ -154,6 +154,12 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
*/
SDWebImageForceTransition = 1 << 16,

/**
* By default, we will decode the image in the background during cache query and download from the network. This can help to improve performance because when rendering image on the screen, it need to be firstly decoded. But this happen on the main queue by Core Animation.
* However, this process may increase the memory usage as well. If you are experiencing a issue due to excessive memory consumption, This flag can prevent decode the image.
*/
SDWebImageAvoidDecodeImage = 1 << 17,

/**
* By default, we decode the animated image. This flag can force decode the first frame only and produece the static image.
*/
Expand Down
14 changes: 11 additions & 3 deletions SDWebImage/SDWebImageDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,27 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
SDWebImageDownloaderHighPriority = 1 << 7,

/**
* Scale down the image
* By default, images are decoded respecting their original size. On iOS, this flag will scale down the
* images to a size compatible with the constrained memory of devices.
* This flag take no effect if `SDWebImageDownloaderAvoidDecodeImage` is set. And it will be ignored if `SDWebImageDownloaderProgressiveDownload` is set.
*/
SDWebImageDownloaderScaleDownLargeImages = 1 << 8,

/**
* By default, we will decode the image in the background during cache query and download from the network. This can help to improve performance because when rendering image on the screen, it need to be firstly decoded. But this happen on the main queue by Core Animation.
* However, this process may increase the memory usage as well. If you are experiencing a issue due to excessive memory consumption, This flag can prevent decode the image.
*/
SDWebImageDownloaderAvoidDecodeImage = 1 << 9,

/**
* By default, we decode the animated image. This flag can force decode the first frame only and produece the static image.
*/
SDWebImageDownloaderDecodeFirstFrameOnly = 1 << 9,
SDWebImageDownloaderDecodeFirstFrameOnly = 1 << 10,

/**
* By default, for `SDAnimatedImage`, we decode the animated image frame during rendering to reduce memory usage. This flag actually trigger `preloadAllAnimatedImageFrames = YES` after image load from network
*/
SDWebImageDownloaderPreloadAllFrames = 1 << 10
SDWebImageDownloaderPreloadAllFrames = 1 << 11
};

FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadStartNotification;
Expand Down
1 change: 0 additions & 1 deletion SDWebImage/SDWebImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
operationClass = [SDWebImageDownloaderOperation class];
}
NSOperation<SDWebImageDownloaderOperation> *operation = [[operationClass alloc] initWithRequest:request inSession:sself.session options:options context:context];
operation.shouldDecompressImages = sself.config.shouldDecompressImages;

if (sself.config.urlCredential) {
operation.credential = sself.config.urlCredential;
Expand Down
6 changes: 0 additions & 6 deletions SDWebImage/SDWebImageDownloaderConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
*/
@property (nonatomic, class, nonnull) SDWebImageDownloaderConfig *defaultDownloaderConfig;

/**
* Decompressing images that are downloaded and cached can improve performance but can consume lot of memory.
* Defaults to YES. Set this to NO if you are experiencing a crash due to excessive memory consumption.
*/
@property (nonatomic, assign) BOOL shouldDecompressImages;

/**
* The maximum number of concurrent downloads.
* Defaults to 6.
Expand Down
2 changes: 0 additions & 2 deletions SDWebImage/SDWebImageDownloaderConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ + (void)setDefaultDownloaderConfig:(SDWebImageDownloaderConfig *)defaultDownload
- (instancetype)init {
self = [super init];
if (self) {
_shouldDecompressImages = YES;
_maxConcurrentDownloads = 6;
_downloadTimeout = 15.0;
_executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
Expand All @@ -39,7 +38,6 @@ - (instancetype)init {

- (id)copyWithZone:(NSZone *)zone {
SDWebImageDownloaderConfig *config = [[[self class] allocWithZone:zone] init];
config.shouldDecompressImages = self.shouldDecompressImages;
config.maxConcurrentDownloads = self.maxConcurrentDownloads;
config.downloadTimeout = self.downloadTimeout;
config.sessionConfiguration = [self.sessionConfiguration copyWithZone:zone];
Expand Down
9 changes: 0 additions & 9 deletions SDWebImage/SDWebImageDownloaderOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
- (nullable id)addHandlersForProgress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;

- (BOOL)shouldDecompressImages;
- (void)setShouldDecompressImages:(BOOL)value;

- (nullable NSURLCredential *)credential;
- (void)setCredential:(nullable NSURLCredential *)value;

Expand Down Expand Up @@ -70,12 +67,6 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
*/
@property (strong, nonatomic, readonly, nullable) NSURLSessionTask *dataTask;

/**
* Decompressing images that are downloaded and cached can improve performance but can consume lot of memory.
* Defaults to YES. Set this to NO if you are experiencing a crash due to excessive memory consumption.
*/
@property (assign, nonatomic) BOOL shouldDecompressImages;

/**
* The credential used for authentication challenges in `-URLSession:task:didReceiveChallenge:completionHandler:`.
*
Expand Down
5 changes: 2 additions & 3 deletions SDWebImage/SDWebImageDownloaderOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
context:(nullable SDWebImageContext *)context {
if ((self = [super init])) {
_request = [request copy];
_shouldDecompressImages = YES;
_options = options;
_context = [context copy];
_callbackBlocks = [NSMutableArray new];
Expand Down Expand Up @@ -379,7 +378,7 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data
image = [self.progressiveCoder incrementalDecodedImageWithOptions:@{SDWebImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDWebImageCoderDecodeScaleFactor : @(scale)}];
}
if (image) {
BOOL shouldDecode = self.shouldDecompressImages;
BOOL shouldDecode = (self.options & SDWebImageDownloaderAvoidDecodeImage) == 0;
if ([image conformsToProtocol:@protocol(SDAnimatedImage)]) {
// `SDAnimatedImage` do not decode
shouldDecode = NO;
Expand Down Expand Up @@ -479,7 +478,7 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
image = [[SDWebImageCodersManager sharedManager] decodedImageWithData:imageData options:@{SDWebImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDWebImageCoderDecodeScaleFactor : @(scale)}];
}

BOOL shouldDecode = self.shouldDecompressImages;
BOOL shouldDecode = (self.options & SDWebImageDownloaderAvoidDecodeImage) == 0;
if ([image conformsToProtocol:@protocol(SDAnimatedImage)]) {
// `SDAnimatedImage` do not decode
shouldDecode = NO;
Expand Down
18 changes: 9 additions & 9 deletions SDWebImage/UIImage+ForceDecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@
@property (nonatomic, assign) BOOL sd_isDecoded;

/**
Decompress (force decode before rendering) the provided image
Decode the provided image. This is useful if you want to force decode the image before rendering to improve performance.

@param image The image to be decompressed
@return The decompressed image
@param image The image to be decoded
@return The decoded image
*/
+ (nullable UIImage *)sd_decodedImageWithImage:(nullable UIImage *)image;

/**
Decompress and scale down the provided image
Decode and scale down the provided image

@param image The image to be decompressed
@return The decompressed and scaled down image
@param image The image to be decoded
@return The decoded and scaled down image
*/
+ (nullable UIImage *)sd_decodedAndScaledDownImageWithImage:(nullable UIImage *)image;

/**
Decompress and scale down the provided image and limit bytes
Decode and scale down the provided image with limit bytes

@param image The image to be decompressed
@param image The image to be decoded
@param bytes The limit bytes size. Provide 0 to use the build-in limit.
@return The decompressed and scaled down image
@return The decoded and scaled down image
*/
+ (nullable UIImage *)sd_decodedAndScaledDownImageWithImage:(nullable UIImage *)image limitBytes:(NSUInteger)bytes;

Expand Down
1 change: 0 additions & 1 deletion Tests/Tests/SDWebImageTestDownloadOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
@interface SDWebImageTestDownloadOperation : NSOperation <SDWebImageDownloaderOperation>

@property (nonatomic, assign) BOOL shouldDecompressImages;
@property (nonatomic, strong, nullable) NSURLCredential *credential;
@property (nonatomic, strong, nullable) NSURLRequest *request;
@property (nonatomic, strong, nullable) NSURLResponse *response;
Expand Down