Fix the SDAnimatedImageRep which use the deprecated API and cause compile issue on Xcode 11. Using self-created CGImageSource instance instead of super class implementation detail

This commit is contained in:
DreamPiggy 2019-06-05 20:31:31 +08:00
parent 428899a041
commit 4aa1181cd1
1 changed files with 27 additions and 12 deletions

View File

@ -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