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; @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; @property (nonatomic, copy, readonly, nullable) NSData *animatedImageData;

View File

@ -350,24 +350,50 @@ static NSArray *SDBundlePreferredScales() {
#pragma mark - NSSecureCoding #pragma mark - NSSecureCoding
- (instancetype)initWithCoder:(NSCoder *)aDecoder { - (instancetype)initWithCoder:(NSCoder *)aDecoder {
NSNumber *scale = [aDecoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(scale))]; self = [super initWithCoder:aDecoder];
NSData *animatedImageData = [aDecoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(animatedImageData))]; if (self) {
if (animatedImageData) { NSData *animatedImageData = [aDecoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(animatedImageData))];
#pragma clang diagnostic push CGFloat scale;
#pragma clang diagnostic ignored "-Wobjc-designated-initializers" #if SD_MAC
return [self initWithData:animatedImageData scale:scale.doubleValue]; NSNumber *scaleValue = [aDecoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(scale))];
#pragma clang diagnostic pop scale = scaleValue.doubleValue;
} else { if (scale < 1) {
return [super initWithCoder:aDecoder]; 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 { - (void)encodeWithCoder:(NSCoder *)aCoder {
if (self.animatedImageData) { [super encodeWithCoder:aCoder];
[aCoder encodeObject:self.animatedImageData forKey:NSStringFromSelector(@selector(animatedImageData))]; #if SD_MAC
[aCoder encodeObject:@(self.scale) forKey:NSStringFromSelector(@selector(scale))]; [aCoder encodeObject:@(self.scale) forKey:NSStringFromSelector(@selector(scale))];
} else { #endif
[super encodeWithCoder:aCoder]; NSData *animatedImageData = self.animatedImageData;
if (animatedImageData) {
[aCoder encodeObject:animatedImageData forKey:NSStringFromSelector(@selector(animatedImageData))];
} }
} }