diff --git a/SDWebImage/SDAnimatedImage.h b/SDWebImage/SDAnimatedImage.h index 00f2fb7f..a98bd302 100644 --- a/SDWebImage/SDAnimatedImage.h +++ b/SDWebImage/SDAnimatedImage.h @@ -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; diff --git a/SDWebImage/SDAnimatedImage.m b/SDWebImage/SDAnimatedImage.m index 74e16784..30eedbb4 100644 --- a/SDWebImage/SDAnimatedImage.m +++ b/SDWebImage/SDAnimatedImage.m @@ -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 animatedCoder = nil; + for (idcoder 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))]; } }