2016-06-01 20:53:08 +08:00
|
|
|
/*
|
|
|
|
* This file is part of the SDWebImage package.
|
|
|
|
* (c) Olivier Poitrey <rs@dailymotion.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
2013-06-08 01:39:07 +08:00
|
|
|
|
|
|
|
#import "UIImage+MultiFormat.h"
|
2018-04-26 22:51:50 +08:00
|
|
|
#import "SDImageCodersManager.h"
|
2013-06-08 01:39:07 +08:00
|
|
|
|
|
|
|
@implementation UIImage (MultiFormat)
|
|
|
|
|
Feature: refactor decoding code and provide decoding plugin
- open the decoding/encoding procedures to the users
- switch from hardcoded decoding/encoding to pluginable decoders/encoders (builtin + user created)
- `SDWebImageCodersManager` is a singleton holding an array of `SDImageCoder` (protocol). Even though a singleton is not necesarily a good pattern, in this case it eliminates dealing with passing this array around
- uses a priority queue behind scenes, which means the latest added coders have priority.
- the priority is crucial when encoding/decoding something, we go through the list and ask each coder if they can handle the current data (see `canDecodeFromData:`, `canEncodeToFormat:`, `canIncrementallyDecodeFromData:`)
- each coder must conform to this protocol `SDImageCoder` describing all the required behavior for a coder
- we provide 3 built-in coders: `SDWebImageImageIOCoder` (for JPEG, PNG, TIFF), `SDWebImageGIFCoder` (for GIF), `SDWebImageWebPCoder` (for WebP and animated WebP)
- the user of SDWebImage can create custom coders by conforming to `SDImageCoder` and adding the coders to `SDWebImageCodersManager`. See `addCoder:` or `removeCoder:` or `coders` getter to get the array
- in order to preserve backwards compatibility, the UIImage categories were preserved, calling the new coders APIs described above
2017-10-17 00:52:18 +08:00
|
|
|
+ (nullable UIImage *)sd_imageWithData:(nullable NSData *)data {
|
2018-04-13 02:34:52 +08:00
|
|
|
return [self sd_imageWithData:data scale:1];
|
2018-01-24 00:44:44 +08:00
|
|
|
}
|
|
|
|
|
2018-04-13 02:34:52 +08:00
|
|
|
+ (nullable UIImage *)sd_imageWithData:(nullable NSData *)data scale:(CGFloat)scale {
|
2018-06-15 23:52:18 +08:00
|
|
|
return [self sd_imageWithData:data scale:scale firstFrameOnly:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (nullable UIImage *)sd_imageWithData:(nullable NSData *)data scale:(CGFloat)scale firstFrameOnly:(BOOL)firstFrameOnly {
|
2018-01-24 00:44:44 +08:00
|
|
|
if (!data) {
|
|
|
|
return nil;
|
|
|
|
}
|
2019-04-26 16:35:54 +08:00
|
|
|
SDImageCoderOptions *options = @{SDImageCoderDecodeScaleFactor : @(MAX(scale, 1)), SDImageCoderDecodeFirstFrameOnly : @(firstFrameOnly)};
|
2018-04-26 22:51:50 +08:00
|
|
|
return [[SDImageCodersManager sharedManager] decodedImageWithData:data options:options];
|
2014-06-17 23:38:37 +08:00
|
|
|
}
|
|
|
|
|
2016-09-24 01:26:46 +08:00
|
|
|
- (nullable NSData *)sd_imageData {
|
|
|
|
return [self sd_imageDataAsFormat:SDImageFormatUndefined];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat {
|
2018-01-24 00:44:44 +08:00
|
|
|
return [self sd_imageDataAsFormat:imageFormat compressionQuality:1];
|
2016-09-24 01:26:46 +08:00
|
|
|
}
|
|
|
|
|
2018-01-24 00:44:44 +08:00
|
|
|
- (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat compressionQuality:(double)compressionQuality {
|
2018-06-15 23:52:18 +08:00
|
|
|
return [self sd_imageDataAsFormat:imageFormat compressionQuality:compressionQuality firstFrameOnly:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat compressionQuality:(double)compressionQuality firstFrameOnly:(BOOL)firstFrameOnly {
|
|
|
|
SDImageCoderOptions *options = @{SDImageCoderEncodeCompressionQuality : @(compressionQuality), SDImageCoderEncodeFirstFrameOnly : @(firstFrameOnly)};
|
2018-04-26 22:51:50 +08:00
|
|
|
return [[SDImageCodersManager sharedManager] encodedDataWithImage:self format:imageFormat options:options];
|
2018-01-24 00:44:44 +08:00
|
|
|
}
|
2014-06-17 23:38:37 +08:00
|
|
|
|
2013-06-08 01:39:07 +08:00
|
|
|
@end
|