Add one convenient method to use base64 encoded image data

This commit is contained in:
DreamPiggy 2019-10-15 15:02:03 +08:00
parent 68d73f4cb2
commit c0602e6733
4 changed files with 29 additions and 2 deletions

View File

@ -234,7 +234,7 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextAnimat
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextDownloadRequestModifier;
/**
A id<SDWebImageDownloaderResponseModifier> instance to modify the image download response and download data. It's used for downloader to modify the original response and data from URL and options. This can be used for image data decryption, such as Gzipped image. If you provide one, it will ignore the `responseModifier` in downloader and use provided one instead. (id<SDWebImageDownloaderResponseModifier>)
A id<SDWebImageDownloaderResponseModifier> instance to modify the image download response and download data. It's used for downloader to modify the original response and data from URL and options. This can be used for image data decryption, such as Base64 encoded image. If you provide one, it will ignore the `responseModifier` in downloader and use provided one instead. (id<SDWebImageDownloaderResponseModifier>)
*/
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextDownloadResponseModifier;

View File

@ -150,7 +150,7 @@ typedef SDImageLoaderCompletedBlock SDWebImageDownloaderCompletedBlock;
@property (nonatomic, strong, nullable) id<SDWebImageDownloaderRequestModifier> requestModifier;
/**
* Set the response modifier to modify the original download response or download data before decoding. This can be used for image data decryption, such as Gzipped image.
* Set the response modifier to modify the original download response or download data before decoding. This can be used for image data decryption, such as Base64 encoded image.
* This request modifier method will be called for each downloading image response. Return the original response or data means no modication. Return nil from either response or data, will mark this download failed, pay attention to this.
* Defaults to nil, means does not modify the original download response or download data.
* @note If you want to modify single response, consider using `SDWebImageContextDownloadResponseModifier` context option.

View File

@ -32,3 +32,12 @@ typedef NSData * _Nullable (^SDWebImageDownloaderResponseModifierDataBlock)(NSDa
+ (nonnull instancetype)responseModifierWithResponseBlock:(nonnull SDWebImageDownloaderResponseModifierBlock)responseBlock dataBlock:(nonnull SDWebImageDownloaderResponseModifierDataBlock)dataBlock;
@end
/// Convenience way to create response modifier for common data encryption.
@interface SDWebImageDownloaderResponseModifier (Conveniences)
/// Base64 Encoded image data
@property (class, readonly, nonnull) SDWebImageDownloaderResponseModifier *base64ResponseModifier;
@end

View File

@ -47,3 +47,21 @@
}
@end
@implementation SDWebImageDownloaderResponseModifier (Conveniences)
+ (SDWebImageDownloaderResponseModifier *)base64ResponseModifier {
static SDWebImageDownloaderResponseModifier *responseModifier;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
responseModifier = [SDWebImageDownloaderResponseModifier responseModifierWithResponseBlock:^NSURLResponse * _Nullable(NSURLResponse * _Nonnull response) {
return response;
} dataBlock:^NSData * _Nullable(NSData * _Nonnull data, NSURLResponse * _Nullable response) {
return [[NSData alloc] initWithBase64EncodedData:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
}];
});
return responseModifier;
}
@end