Fix the NSSecureCoding implementation for SDAnimatedImage

This commit is contained in:
DreamPiggy 2018-04-15 01:36:10 +08:00
parent fcfca57463
commit 94b67bf3b0
2 changed files with 42 additions and 15 deletions

View File

@ -69,7 +69,8 @@
@property (nonatomic, assign, readonly) SDImageFormat animatedImageFormat;
/**
Current animated image data, you can use this instead of CGImage to create another instance
Current animated image data, you can use this instead of CGImage to create another instance.
If the current image is not animated image, this value is nil.
*/
@property (nonatomic, copy, readonly, nullable) NSData *animatedImageData;

View File

@ -350,24 +350,50 @@ static NSArray *SDBundlePreferredScales() {
#pragma mark - NSSecureCoding
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
NSNumber *scale = [aDecoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(scale))];
NSData *animatedImageData = [aDecoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(animatedImageData))];
if (animatedImageData) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-designated-initializers"
return [self initWithData:animatedImageData scale:scale.doubleValue];
#pragma clang diagnostic pop
} else {
return [super initWithCoder:aDecoder];
self = [super initWithCoder:aDecoder];
if (self) {
NSData *animatedImageData = [aDecoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(animatedImageData))];
CGFloat scale;
#if SD_MAC
NSNumber *scaleValue = [aDecoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(scale))];
scale = scaleValue.doubleValue;
if (scale < 1) {
scale = 1;
}
_scale = scale;
#else
scale = self.scale;
#endif
if (!animatedImageData) {
return self;
}
id<SDWebImageAnimatedCoder> animatedCoder = nil;
for (id<SDWebImageCoder>coder in [SDWebImageCodersManager sharedManager].coders) {
if ([coder conformsToProtocol:@protocol(SDWebImageAnimatedCoder)]) {
if ([coder canDecodeFromData:animatedImageData]) {
animatedCoder = [[[coder class] alloc] initWithAnimatedImageData:animatedImageData options:@{SDWebImageCoderDecodeScaleFactor : @(scale)}];
break;
}
}
}
if (!animatedCoder) {
return self;
}
_coder = animatedCoder;
SDImageFormat format = [NSData sd_imageFormatForImageData:animatedImageData];
_animatedImageFormat = format;
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
if (self.animatedImageData) {
[aCoder encodeObject:self.animatedImageData forKey:NSStringFromSelector(@selector(animatedImageData))];
[aCoder encodeObject:@(self.scale) forKey:NSStringFromSelector(@selector(scale))];
} else {
[super encodeWithCoder:aCoder];
[super encodeWithCoder:aCoder];
#if SD_MAC
[aCoder encodeObject:@(self.scale) forKey:NSStringFromSelector(@selector(scale))];
#endif
NSData *animatedImageData = self.animatedImageData;
if (animatedImageData) {
[aCoder encodeObject:animatedImageData forKey:NSStringFromSelector(@selector(animatedImageData))];
}
}