diff --git a/SDWebImage/SDAnimatedImageRep.m b/SDWebImage/SDAnimatedImageRep.m index f0907d5c..40e57855 100644 --- a/SDWebImage/SDAnimatedImageRep.m +++ b/SDWebImage/SDAnimatedImageRep.m @@ -18,20 +18,42 @@ @end -@interface SDAnimatedImageRep () +@implementation SDAnimatedImageRep { + CGImageSourceRef _imageSource; +} -@property (nonatomic, assign, readonly, nullable) CGImageSourceRef imageSource; +- (void)dealloc { + if (_imageSource) { + CFRelease(_imageSource); + _imageSource = NULL; + } +} -@end +// `NSBitmapImageRep`'s `imageRepWithData:` is not designed initlizer ++ (instancetype)imageRepWithData:(NSData *)data { + SDAnimatedImageRep *imageRep = [[SDAnimatedImageRep alloc] initWithData:data]; + return imageRep; +} -@implementation SDAnimatedImageRep +// We should override init method for `NSBitmapImageRep` to do initlize about animated image format +- (instancetype)initWithData:(NSData *)data { + self = [super initWithData:data]; + if (self) { + CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef) data, NULL); + if (!imageSource) { + return self; + } + _imageSource = imageSource; + } + return self; +} // `NSBitmapImageRep` will use `kCGImagePropertyGIFDelayTime` whenever you call `setProperty:withValue:` with `NSImageCurrentFrame` to change the current frame. We override it and use the actual `kCGImagePropertyGIFUnclampedDelayTime` if need. - (void)setProperty:(NSBitmapImageRepPropertyKey)property withValue:(id)value { [super setProperty:property withValue:value]; if ([property isEqualToString:NSImageCurrentFrame]) { // Access the image source - CGImageSourceRef imageSource = self.imageSource; + CGImageSourceRef imageSource = _imageSource; if (!imageSource) { return; } @@ -54,13 +76,6 @@ } } -- (CGImageSourceRef)imageSource { - if (_tiffData) { - return (__bridge CGImageSourceRef)(_tiffData); - } - return NULL; -} - @end #endif