Refactory current GIF/APNG coder, use abstract base class `SDImageIOAnimatedCoder` for subclass to implements the required method.
This commit is contained in:
parent
f53b0512a2
commit
2aa4e7d8a7
|
@ -75,8 +75,6 @@
|
|||
325C460F223394D8004CAE11 /* SDImageCachesManagerOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 325C460C223394D8004CAE11 /* SDImageCachesManagerOperation.h */; settings = {ATTRIBUTES = (Private, ); }; };
|
||||
325C4610223394D8004CAE11 /* SDImageCachesManagerOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 325C460D223394D8004CAE11 /* SDImageCachesManagerOperation.m */; };
|
||||
325C4611223394D8004CAE11 /* SDImageCachesManagerOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 325C460D223394D8004CAE11 /* SDImageCachesManagerOperation.m */; };
|
||||
325C4615223399F7004CAE11 /* SDImageGIFCoderInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 325C4612223399F7004CAE11 /* SDImageGIFCoderInternal.h */; settings = {ATTRIBUTES = (Private, ); }; };
|
||||
325C461B22339B5F004CAE11 /* SDImageAPNGCoderInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 325C461822339B5F004CAE11 /* SDImageAPNGCoderInternal.h */; settings = {ATTRIBUTES = (Private, ); }; };
|
||||
325C46212233A02E004CAE11 /* UIColor+HexString.h in Headers */ = {isa = PBXBuildFile; fileRef = 325C461E2233A02E004CAE11 /* UIColor+HexString.h */; settings = {ATTRIBUTES = (Private, ); }; };
|
||||
325C46222233A02E004CAE11 /* UIColor+HexString.m in Sources */ = {isa = PBXBuildFile; fileRef = 325C461F2233A02E004CAE11 /* UIColor+HexString.m */; };
|
||||
325C46232233A02E004CAE11 /* UIColor+HexString.m in Sources */ = {isa = PBXBuildFile; fileRef = 325C461F2233A02E004CAE11 /* UIColor+HexString.m */; };
|
||||
|
@ -158,6 +156,9 @@
|
|||
329F1240223FAD3400B309FD /* SDInternalMacros.m in Sources */ = {isa = PBXBuildFile; fileRef = 329F123E223FAD3400B309FD /* SDInternalMacros.m */; };
|
||||
329F1241223FAD3400B309FD /* SDInternalMacros.m in Sources */ = {isa = PBXBuildFile; fileRef = 329F123E223FAD3400B309FD /* SDInternalMacros.m */; };
|
||||
329F1243223FAD3400B309FD /* SDInternalMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 329F123F223FAD3400B309FD /* SDInternalMacros.h */; settings = {ATTRIBUTES = (Private, ); }; };
|
||||
32A09E3F233358B700339F9D /* SDImageIOAnimatedCoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 32A09E3D233358B700339F9D /* SDImageIOAnimatedCoder.h */; };
|
||||
32A09E41233358B700339F9D /* SDImageIOAnimatedCoder.m in Sources */ = {isa = PBXBuildFile; fileRef = 32A09E3E233358B700339F9D /* SDImageIOAnimatedCoder.m */; };
|
||||
32A09E42233358B700339F9D /* SDImageIOAnimatedCoder.m in Sources */ = {isa = PBXBuildFile; fileRef = 32A09E3E233358B700339F9D /* SDImageIOAnimatedCoder.m */; };
|
||||
32B5CC60222F89C2005EB74E /* SDAsyncBlockOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 32B5CC5E222F89C2005EB74E /* SDAsyncBlockOperation.h */; settings = {ATTRIBUTES = (Private, ); }; };
|
||||
32B5CC61222F89C2005EB74E /* SDAsyncBlockOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 32B5CC5F222F89C2005EB74E /* SDAsyncBlockOperation.m */; };
|
||||
32B5CC63222F8B70005EB74E /* SDAsyncBlockOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 32B5CC5F222F89C2005EB74E /* SDAsyncBlockOperation.m */; };
|
||||
|
@ -167,6 +168,8 @@
|
|||
32C0FDE32013426C001B8F2D /* SDWebImageIndicator.h in Headers */ = {isa = PBXBuildFile; fileRef = 32C0FDDF2013426C001B8F2D /* SDWebImageIndicator.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
32C0FDE72013426C001B8F2D /* SDWebImageIndicator.m in Sources */ = {isa = PBXBuildFile; fileRef = 32C0FDE02013426C001B8F2D /* SDWebImageIndicator.m */; };
|
||||
32C0FDE92013426C001B8F2D /* SDWebImageIndicator.m in Sources */ = {isa = PBXBuildFile; fileRef = 32C0FDE02013426C001B8F2D /* SDWebImageIndicator.m */; };
|
||||
32C78E3823336FC800C6B7F8 /* SDImageIOAnimatedCoder.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 32A09E3D233358B700339F9D /* SDImageIOAnimatedCoder.h */; };
|
||||
32C78E3B233371AD00C6B7F8 /* SDImageIOAnimatedCoderInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 32C78E39233371AD00C6B7F8 /* SDImageIOAnimatedCoderInternal.h */; settings = {ATTRIBUTES = (Private, ); }; };
|
||||
32CF1C091FA496B000004BD1 /* SDImageCoderHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 32CF1C051FA496B000004BD1 /* SDImageCoderHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
32CF1C0D1FA496B000004BD1 /* SDImageCoderHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 32CF1C061FA496B000004BD1 /* SDImageCoderHelper.m */; };
|
||||
32CF1C0F1FA496B000004BD1 /* SDImageCoderHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 32CF1C061FA496B000004BD1 /* SDImageCoderHelper.m */; };
|
||||
|
@ -272,6 +275,7 @@
|
|||
dstPath = include/SDWebImage;
|
||||
dstSubfolderSpec = 16;
|
||||
files = (
|
||||
32C78E3823336FC800C6B7F8 /* SDImageIOAnimatedCoder.h in Copy Headers */,
|
||||
32E5690822B1FFCA00CBABC6 /* SDWebImageOptionsProcessor.h in Copy Headers */,
|
||||
32935D2F22A4FEE50049C068 /* SDWebImage.h in Copy Headers */,
|
||||
32935CFE22A4FEDE0049C068 /* SDWebImageManager.h in Copy Headers */,
|
||||
|
@ -368,8 +372,6 @@
|
|||
325C460722339426004CAE11 /* SDWeakProxy.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDWeakProxy.m; sourceTree = "<group>"; };
|
||||
325C460C223394D8004CAE11 /* SDImageCachesManagerOperation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDImageCachesManagerOperation.h; sourceTree = "<group>"; };
|
||||
325C460D223394D8004CAE11 /* SDImageCachesManagerOperation.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDImageCachesManagerOperation.m; sourceTree = "<group>"; };
|
||||
325C4612223399F7004CAE11 /* SDImageGIFCoderInternal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDImageGIFCoderInternal.h; sourceTree = "<group>"; };
|
||||
325C461822339B5F004CAE11 /* SDImageAPNGCoderInternal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDImageAPNGCoderInternal.h; sourceTree = "<group>"; };
|
||||
325C461E2233A02E004CAE11 /* UIColor+HexString.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIColor+HexString.h"; sourceTree = "<group>"; };
|
||||
325C461F2233A02E004CAE11 /* UIColor+HexString.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIColor+HexString.m"; sourceTree = "<group>"; };
|
||||
325C46242233A0A8004CAE11 /* NSBezierPath+RoundedCorners.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSBezierPath+RoundedCorners.h"; sourceTree = "<group>"; };
|
||||
|
@ -391,12 +393,15 @@
|
|||
329F1235223FAA3B00B309FD /* SDmetamacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDmetamacros.h; sourceTree = "<group>"; };
|
||||
329F123E223FAD3400B309FD /* SDInternalMacros.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDInternalMacros.m; sourceTree = "<group>"; };
|
||||
329F123F223FAD3400B309FD /* SDInternalMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDInternalMacros.h; sourceTree = "<group>"; };
|
||||
32A09E3D233358B700339F9D /* SDImageIOAnimatedCoder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SDImageIOAnimatedCoder.h; path = Core/SDImageIOAnimatedCoder.h; sourceTree = "<group>"; };
|
||||
32A09E3E233358B700339F9D /* SDImageIOAnimatedCoder.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = SDImageIOAnimatedCoder.m; path = Core/SDImageIOAnimatedCoder.m; sourceTree = "<group>"; };
|
||||
32B5CC5E222F89C2005EB74E /* SDAsyncBlockOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDAsyncBlockOperation.h; sourceTree = "<group>"; };
|
||||
32B5CC5F222F89C2005EB74E /* SDAsyncBlockOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDAsyncBlockOperation.m; sourceTree = "<group>"; };
|
||||
32B9B535206ED4230026769D /* SDWebImageDownloaderConfig.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SDWebImageDownloaderConfig.h; path = Core/SDWebImageDownloaderConfig.h; sourceTree = "<group>"; };
|
||||
32B9B536206ED4230026769D /* SDWebImageDownloaderConfig.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = SDWebImageDownloaderConfig.m; path = Core/SDWebImageDownloaderConfig.m; sourceTree = "<group>"; };
|
||||
32C0FDDF2013426C001B8F2D /* SDWebImageIndicator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SDWebImageIndicator.h; path = Core/SDWebImageIndicator.h; sourceTree = "<group>"; };
|
||||
32C0FDE02013426C001B8F2D /* SDWebImageIndicator.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = SDWebImageIndicator.m; path = Core/SDWebImageIndicator.m; sourceTree = "<group>"; };
|
||||
32C78E39233371AD00C6B7F8 /* SDImageIOAnimatedCoderInternal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDImageIOAnimatedCoderInternal.h; sourceTree = "<group>"; };
|
||||
32CF1C051FA496B000004BD1 /* SDImageCoderHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SDImageCoderHelper.h; path = Core/SDImageCoderHelper.h; sourceTree = "<group>"; };
|
||||
32CF1C061FA496B000004BD1 /* SDImageCoderHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = SDImageCoderHelper.m; path = Core/SDImageCoderHelper.m; sourceTree = "<group>"; };
|
||||
32D1221A2080B2EB003685A3 /* SDImageCacheDefine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDImageCacheDefine.h; path = Core/SDImageCacheDefine.h; sourceTree = "<group>"; };
|
||||
|
@ -501,6 +506,8 @@
|
|||
321E60851F38E8C800405457 /* SDImageCoder.m */,
|
||||
321E60921F38E8ED00405457 /* SDImageIOCoder.h */,
|
||||
321E60931F38E8ED00405457 /* SDImageIOCoder.m */,
|
||||
32A09E3D233358B700339F9D /* SDImageIOAnimatedCoder.h */,
|
||||
32A09E3E233358B700339F9D /* SDImageIOAnimatedCoder.m */,
|
||||
321E60A01F38E8F600405457 /* SDImageGIFCoder.h */,
|
||||
321E60A11F38E8F600405457 /* SDImageGIFCoder.m */,
|
||||
327054D2206CD8B3006EA328 /* SDImageAPNGCoder.h */,
|
||||
|
@ -574,8 +581,7 @@
|
|||
325C460122339330004CAE11 /* SDImageAssetManager.m */,
|
||||
325C460C223394D8004CAE11 /* SDImageCachesManagerOperation.h */,
|
||||
325C460D223394D8004CAE11 /* SDImageCachesManagerOperation.m */,
|
||||
325C4612223399F7004CAE11 /* SDImageGIFCoderInternal.h */,
|
||||
325C461822339B5F004CAE11 /* SDImageAPNGCoderInternal.h */,
|
||||
32C78E39233371AD00C6B7F8 /* SDImageIOAnimatedCoderInternal.h */,
|
||||
325C461E2233A02E004CAE11 /* UIColor+HexString.h */,
|
||||
325C461F2233A02E004CAE11 /* UIColor+HexString.m */,
|
||||
325C46242233A0A8004CAE11 /* NSBezierPath+RoundedCorners.h */,
|
||||
|
@ -796,12 +802,14 @@
|
|||
321E60961F38E8ED00405457 /* SDImageIOCoder.h in Headers */,
|
||||
4A2CAE041AB4BB5400B6BC39 /* SDWebImage.h in Headers */,
|
||||
325C460322339330004CAE11 /* SDImageAssetManager.h in Headers */,
|
||||
32C78E3B233371AD00C6B7F8 /* SDImageIOAnimatedCoderInternal.h in Headers */,
|
||||
327054D6206CD8B3006EA328 /* SDImageAPNGCoder.h in Headers */,
|
||||
80B6DF842142B44600BCB334 /* NSButton+WebCache.h in Headers */,
|
||||
43A918661D8308FE00B3925F /* SDImageCacheConfig.h in Headers */,
|
||||
3290FA061FA478AF0047D20C /* SDImageFrame.h in Headers */,
|
||||
329F1237223FAA3B00B309FD /* SDmetamacros.h in Headers */,
|
||||
324DF4B6200A14DC008A84CC /* SDWebImageDefine.h in Headers */,
|
||||
32A09E3F233358B700339F9D /* SDImageIOAnimatedCoder.h in Headers */,
|
||||
807A122A1F89636300EC2A9B /* SDImageCodersManager.h in Headers */,
|
||||
3244062C2296C5F400A36084 /* SDWebImageOptionsProcessor.h in Headers */,
|
||||
4A2CAE211AB4BB7000B6BC39 /* SDWebImageManager.h in Headers */,
|
||||
|
@ -816,7 +824,6 @@
|
|||
4A2CAE251AB4BB7000B6BC39 /* SDWebImagePrefetcher.h in Headers */,
|
||||
328BB6CF2082581100760D6C /* SDMemoryCache.h in Headers */,
|
||||
325C460F223394D8004CAE11 /* SDImageCachesManagerOperation.h in Headers */,
|
||||
325C461B22339B5F004CAE11 /* SDImageAPNGCoderInternal.h in Headers */,
|
||||
321E60881F38E8C800405457 /* SDImageCoder.h in Headers */,
|
||||
4A2CAE371AB4BB7500B6BC39 /* UIView+WebCacheOperation.h in Headers */,
|
||||
321B37832083290E00C0EA77 /* SDImageLoader.h in Headers */,
|
||||
|
@ -833,7 +840,6 @@
|
|||
4A2CAE1B1AB4BB6800B6BC39 /* SDWebImageDownloader.h in Headers */,
|
||||
3248476B201775F600AF9E5A /* SDAnimatedImageView.h in Headers */,
|
||||
32D122322080B2EB003685A3 /* SDImageCachesManager.h in Headers */,
|
||||
325C4615223399F7004CAE11 /* SDImageGIFCoderInternal.h in Headers */,
|
||||
32F7C0862030719600873181 /* UIImage+Transform.h in Headers */,
|
||||
321E60C01F38E91700405457 /* UIImage+ForceDecode.h in Headers */,
|
||||
329F1243223FAD3400B309FD /* SDInternalMacros.h in Headers */,
|
||||
|
@ -1060,6 +1066,7 @@
|
|||
32D1222C2080B2EB003685A3 /* SDImageCachesManager.m in Sources */,
|
||||
32B9B53F206ED4230026769D /* SDWebImageDownloaderConfig.m in Sources */,
|
||||
43A9186D1D8308FE00B3925F /* SDImageCacheConfig.m in Sources */,
|
||||
32A09E42233358B700339F9D /* SDImageIOAnimatedCoder.m in Sources */,
|
||||
325C46292233A0A8004CAE11 /* NSBezierPath+RoundedCorners.m in Sources */,
|
||||
3248477D201775F600AF9E5A /* SDAnimatedImageView+WebCache.m in Sources */,
|
||||
321E60AA1F38E8F600405457 /* SDImageGIFCoder.m in Sources */,
|
||||
|
@ -1123,6 +1130,7 @@
|
|||
32D1222A2080B2EB003685A3 /* SDImageCachesManager.m in Sources */,
|
||||
32B9B53D206ED4230026769D /* SDWebImageDownloaderConfig.m in Sources */,
|
||||
43A9186B1D8308FE00B3925F /* SDImageCacheConfig.m in Sources */,
|
||||
32A09E41233358B700339F9D /* SDImageIOAnimatedCoder.m in Sources */,
|
||||
325C46282233A0A8004CAE11 /* NSBezierPath+RoundedCorners.m in Sources */,
|
||||
3248477B201775F600AF9E5A /* SDAnimatedImageView+WebCache.m in Sources */,
|
||||
321E60A81F38E8F600405457 /* SDImageGIFCoder.m in Sources */,
|
||||
|
|
|
@ -10,8 +10,9 @@
|
|||
|
||||
#if SD_MAC
|
||||
|
||||
#import "SDImageGIFCoderInternal.h"
|
||||
#import "SDImageAPNGCoderInternal.h"
|
||||
#import "SDImageIOAnimatedCoderInternal.h"
|
||||
#import "SDImageGIFCoder.h"
|
||||
#import "SDImageAPNGCoder.h"
|
||||
|
||||
@implementation SDAnimatedImageRep {
|
||||
CGImageSourceRef _imageSource;
|
||||
|
@ -55,7 +56,7 @@
|
|||
// Do initilize about frame count, current frame/duration and loop count
|
||||
[self setProperty:NSImageFrameCount withValue:@(frameCount)];
|
||||
[self setProperty:NSImageCurrentFrame withValue:@(0)];
|
||||
NSUInteger loopCount = [[SDImageAPNGCoder sharedCoder] sd_imageLoopCountWithSource:imageSource];
|
||||
NSUInteger loopCount = [SDImageAPNGCoder imageLoopCountWithSource:imageSource];
|
||||
[self setProperty:NSImageLoopCount withValue:@(loopCount)];
|
||||
}
|
||||
}
|
||||
|
@ -77,13 +78,13 @@
|
|||
return;
|
||||
}
|
||||
NSUInteger index = [value unsignedIntegerValue];
|
||||
float frameDuration = 0;
|
||||
NSTimeInterval frameDuration = 0;
|
||||
if (CFStringCompare(type, kUTTypeGIF, 0) == kCFCompareEqualTo) {
|
||||
// GIF
|
||||
frameDuration = [[SDImageGIFCoder sharedCoder] sd_frameDurationAtIndex:index source:imageSource];
|
||||
frameDuration = [SDImageGIFCoder frameDurationAtIndex:index source:imageSource];
|
||||
} else if (CFStringCompare(type, kUTTypePNG, 0) == kCFCompareEqualTo) {
|
||||
// APNG
|
||||
frameDuration = [[SDImageAPNGCoder sharedCoder] sd_frameDurationAtIndex:index source:imageSource];
|
||||
frameDuration = [SDImageAPNGCoder frameDurationAtIndex:index source:imageSource];
|
||||
}
|
||||
if (!frameDuration) {
|
||||
return;
|
||||
|
|
|
@ -7,12 +7,12 @@
|
|||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "SDImageCoder.h"
|
||||
#import "SDImageIOAnimatedCoder.h"
|
||||
|
||||
/**
|
||||
Built in coder using ImageIO that supports APNG encoding/decoding
|
||||
*/
|
||||
@interface SDImageAPNGCoder : NSObject <SDProgressiveImageCoder, SDAnimatedImageCoder>
|
||||
@interface SDImageAPNGCoder : SDImageIOAnimatedCoder <SDProgressiveImageCoder, SDAnimatedImageCoder>
|
||||
|
||||
@property (nonatomic, class, readonly, nonnull) SDImageAPNGCoder *sharedCoder;
|
||||
|
||||
|
|
|
@ -7,12 +7,11 @@
|
|||
*/
|
||||
|
||||
#import "SDImageAPNGCoder.h"
|
||||
#import <ImageIO/ImageIO.h>
|
||||
#import "NSData+ImageContentType.h"
|
||||
#import "UIImage+Metadata.h"
|
||||
#import "NSImage+Compatibility.h"
|
||||
#import "SDImageCoderHelper.h"
|
||||
#import "SDAnimatedImageRep.h"
|
||||
#if SD_MAC
|
||||
#import <CoreServices/CoreServices.h>
|
||||
#else
|
||||
#import <MobileCoreServices/MobileCoreServices.h>
|
||||
#endif
|
||||
|
||||
// iOS 8 Image/IO framework binary does not contains these APNG contants, so we define them. Thanks Apple :)
|
||||
#if (__IPHONE_OS_VERSION_MIN_REQUIRED && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0)
|
||||
|
@ -21,46 +20,7 @@ const CFStringRef kCGImagePropertyAPNGDelayTime = (__bridge CFStringRef)@"DelayT
|
|||
const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef)@"UnclampedDelayTime";
|
||||
#endif
|
||||
|
||||
@interface SDAPNGCoderFrame : NSObject
|
||||
|
||||
@property (nonatomic, assign) NSUInteger index; // Frame index (zero based)
|
||||
@property (nonatomic, assign) NSTimeInterval duration; // Frame duration in seconds
|
||||
|
||||
@end
|
||||
|
||||
@implementation SDAPNGCoderFrame
|
||||
@end
|
||||
|
||||
@implementation SDImageAPNGCoder {
|
||||
size_t _width, _height;
|
||||
CGImageSourceRef _imageSource;
|
||||
NSData *_imageData;
|
||||
CGFloat _scale;
|
||||
NSUInteger _loopCount;
|
||||
NSUInteger _frameCount;
|
||||
NSArray<SDAPNGCoderFrame *> *_frames;
|
||||
BOOL _finished;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
if (_imageSource) {
|
||||
CFRelease(_imageSource);
|
||||
_imageSource = NULL;
|
||||
}
|
||||
#if SD_UIKIT
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void)didReceiveMemoryWarning:(NSNotification *)notification
|
||||
{
|
||||
if (_imageSource) {
|
||||
for (size_t i = 0; i < _frameCount; i++) {
|
||||
CGImageSourceRemoveCacheAtIndex(_imageSource, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@implementation SDImageAPNGCoder
|
||||
|
||||
+ (instancetype)sharedCoder {
|
||||
static SDImageAPNGCoder *coder;
|
||||
|
@ -71,346 +31,34 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef
|
|||
return coder;
|
||||
}
|
||||
|
||||
#pragma mark - Decode
|
||||
- (BOOL)canDecodeFromData:(nullable NSData *)data {
|
||||
return ([NSData sd_imageFormatForImageData:data] == SDImageFormatPNG);
|
||||
#pragma mark - Subclass Override
|
||||
|
||||
+ (SDImageFormat)imageFormat {
|
||||
return SDImageFormatPNG;
|
||||
}
|
||||
|
||||
- (UIImage *)decodedImageWithData:(NSData *)data options:(nullable SDImageCoderOptions *)options {
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
CGFloat scale = 1;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
|
||||
#if SD_MAC
|
||||
SDAnimatedImageRep *imageRep = [[SDAnimatedImageRep alloc] initWithData:data];
|
||||
NSSize size = NSMakeSize(imageRep.pixelsWide / scale, imageRep.pixelsHigh / scale);
|
||||
imageRep.size = size;
|
||||
NSImage *animatedImage = [[NSImage alloc] initWithSize:size];
|
||||
[animatedImage addRepresentation:imageRep];
|
||||
return animatedImage;
|
||||
#else
|
||||
|
||||
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
||||
if (!source) {
|
||||
return nil;
|
||||
}
|
||||
size_t count = CGImageSourceGetCount(source);
|
||||
UIImage *animatedImage;
|
||||
|
||||
BOOL decodeFirstFrame = [options[SDImageCoderDecodeFirstFrameOnly] boolValue];
|
||||
if (decodeFirstFrame || count <= 1) {
|
||||
animatedImage = [[UIImage alloc] initWithData:data scale:scale];
|
||||
} else {
|
||||
NSMutableArray<SDImageFrame *> *frames = [NSMutableArray array];
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
|
||||
if (!imageRef) {
|
||||
continue;
|
||||
}
|
||||
|
||||
float duration = [self sd_frameDurationAtIndex:i source:source];
|
||||
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
|
||||
CGImageRelease(imageRef);
|
||||
|
||||
SDImageFrame *frame = [SDImageFrame frameWithImage:image duration:duration];
|
||||
[frames addObject:frame];
|
||||
}
|
||||
|
||||
NSUInteger loopCount = [self sd_imageLoopCountWithSource:source];
|
||||
|
||||
animatedImage = [SDImageCoderHelper animatedImageWithFrames:frames];
|
||||
animatedImage.sd_imageLoopCount = loopCount;
|
||||
}
|
||||
animatedImage.sd_imageFormat = SDImageFormatPNG;
|
||||
CFRelease(source);
|
||||
|
||||
return animatedImage;
|
||||
#endif
|
||||
+ (NSString *)imageUTType {
|
||||
return (__bridge NSString *)kUTTypePNG;
|
||||
}
|
||||
|
||||
- (NSUInteger)sd_imageLoopCountWithSource:(CGImageSourceRef)source {
|
||||
NSUInteger loopCount = 0;
|
||||
NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(source, nil);
|
||||
NSDictionary *pngProperties = imageProperties[(__bridge NSString *)kCGImagePropertyPNGDictionary];
|
||||
if (pngProperties) {
|
||||
NSNumber *apngLoopCount = pngProperties[(__bridge NSString *)kCGImagePropertyAPNGLoopCount];
|
||||
if (apngLoopCount != nil) {
|
||||
loopCount = apngLoopCount.unsignedIntegerValue;
|
||||
}
|
||||
}
|
||||
return loopCount;
|
||||
+ (NSString *)dictionaryProperty {
|
||||
return (__bridge NSString *)kCGImagePropertyPNGDictionary;
|
||||
}
|
||||
|
||||
- (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
|
||||
float frameDuration = 0.1f;
|
||||
CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
|
||||
if (!cfFrameProperties) {
|
||||
return frameDuration;
|
||||
}
|
||||
NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
|
||||
NSDictionary *pngProperties = frameProperties[(NSString *)kCGImagePropertyPNGDictionary];
|
||||
|
||||
NSNumber *delayTimeUnclampedProp = pngProperties[(__bridge NSString *)kCGImagePropertyAPNGUnclampedDelayTime];
|
||||
if (delayTimeUnclampedProp != nil) {
|
||||
frameDuration = [delayTimeUnclampedProp floatValue];
|
||||
} else {
|
||||
NSNumber *delayTimeProp = pngProperties[(__bridge NSString *)kCGImagePropertyAPNGDelayTime];
|
||||
if (delayTimeProp != nil) {
|
||||
frameDuration = [delayTimeProp floatValue];
|
||||
}
|
||||
}
|
||||
|
||||
if (frameDuration < 0.011f) {
|
||||
frameDuration = 0.100f;
|
||||
}
|
||||
|
||||
CFRelease(cfFrameProperties);
|
||||
return frameDuration;
|
||||
+ (NSString *)unclampedDelayTimeProperty {
|
||||
return (__bridge NSString *)kCGImagePropertyAPNGUnclampedDelayTime;
|
||||
}
|
||||
|
||||
#pragma mark - Encode
|
||||
- (BOOL)canEncodeToFormat:(SDImageFormat)format {
|
||||
return (format == SDImageFormatPNG);
|
||||
+ (NSString *)delayTimeProperty {
|
||||
return (__bridge NSString *)kCGImagePropertyAPNGDelayTime;
|
||||
}
|
||||
|
||||
- (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format options:(nullable SDImageCoderOptions *)options {
|
||||
if (!image) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (format != SDImageFormatPNG) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSMutableData *imageData = [NSMutableData data];
|
||||
CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:SDImageFormatPNG];
|
||||
NSArray<SDImageFrame *> *frames = [SDImageCoderHelper framesFromAnimatedImage:image];
|
||||
|
||||
// Create an image destination. APNG does not support EXIF image orientation
|
||||
// The `CGImageDestinationCreateWithData` will log a warning when count is 0, use 1 instead.
|
||||
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageData, imageUTType, frames.count ?: 1, NULL);
|
||||
if (!imageDestination) {
|
||||
// Handle failure.
|
||||
return nil;
|
||||
}
|
||||
NSMutableDictionary *properties = [NSMutableDictionary dictionary];
|
||||
double compressionQuality = 1;
|
||||
if (options[SDImageCoderEncodeCompressionQuality]) {
|
||||
compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue];
|
||||
}
|
||||
properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = @(compressionQuality);
|
||||
|
||||
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];
|
||||
if (encodeFirstFrame || frames.count == 0) {
|
||||
// for static single PNG images
|
||||
CGImageDestinationAddImage(imageDestination, image.CGImage, (__bridge CFDictionaryRef)properties);
|
||||
} else {
|
||||
// for animated APNG images
|
||||
NSUInteger loopCount = image.sd_imageLoopCount;
|
||||
NSDictionary *pngProperties = @{(__bridge NSString *)kCGImagePropertyAPNGLoopCount : @(loopCount)};
|
||||
properties[(__bridge NSString *)kCGImagePropertyPNGDictionary] = pngProperties;
|
||||
CGImageDestinationSetProperties(imageDestination, (__bridge CFDictionaryRef)properties);
|
||||
|
||||
for (size_t i = 0; i < frames.count; i++) {
|
||||
SDImageFrame *frame = frames[i];
|
||||
float frameDuration = frame.duration;
|
||||
CGImageRef frameImageRef = frame.image.CGImage;
|
||||
NSDictionary *frameProperties = @{(__bridge NSString *)kCGImagePropertyPNGDictionary : @{(__bridge NSString *)kCGImagePropertyAPNGDelayTime : @(frameDuration)}};
|
||||
CGImageDestinationAddImage(imageDestination, frameImageRef, (__bridge CFDictionaryRef)frameProperties);
|
||||
}
|
||||
}
|
||||
// Finalize the destination.
|
||||
if (CGImageDestinationFinalize(imageDestination) == NO) {
|
||||
// Handle failure.
|
||||
imageData = nil;
|
||||
}
|
||||
|
||||
CFRelease(imageDestination);
|
||||
|
||||
return [imageData copy];
|
||||
+ (NSString *)loopCountProperty {
|
||||
return (__bridge NSString *)kCGImagePropertyAPNGLoopCount;
|
||||
}
|
||||
|
||||
#pragma mark - Progressive Decode
|
||||
|
||||
- (BOOL)canIncrementalDecodeFromData:(NSData *)data {
|
||||
return ([NSData sd_imageFormatForImageData:data] == SDImageFormatPNG);
|
||||
}
|
||||
|
||||
- (instancetype)initIncrementalWithOptions:(nullable SDImageCoderOptions *)options {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:SDImageFormatPNG];
|
||||
_imageSource = CGImageSourceCreateIncremental((__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageSourceTypeIdentifierHint : (__bridge NSString *)imageUTType});
|
||||
CGFloat scale = 1;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
_scale = scale;
|
||||
#if SD_UIKIT
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
||||
#endif
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)updateIncrementalData:(NSData *)data finished:(BOOL)finished {
|
||||
if (_finished) {
|
||||
return;
|
||||
}
|
||||
_imageData = data;
|
||||
_finished = finished;
|
||||
|
||||
// The following code is from http://www.cocoaintheshell.com/2011/05/progressive-images-download-imageio/
|
||||
// Thanks to the author @Nyx0uf
|
||||
|
||||
// Update the data source, we must pass ALL the data, not just the new bytes
|
||||
CGImageSourceUpdateData(_imageSource, (__bridge CFDataRef)data, finished);
|
||||
|
||||
if (_width + _height == 0) {
|
||||
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(_imageSource, 0, NULL);
|
||||
if (properties) {
|
||||
CFTypeRef val = CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight);
|
||||
if (val) CFNumberGetValue(val, kCFNumberLongType, &_height);
|
||||
val = CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth);
|
||||
if (val) CFNumberGetValue(val, kCFNumberLongType, &_width);
|
||||
CFRelease(properties);
|
||||
}
|
||||
}
|
||||
|
||||
// For animated image progressive decoding because the frame count and duration may be changed.
|
||||
[self scanAndCheckFramesValidWithImageSource:_imageSource];
|
||||
}
|
||||
|
||||
- (UIImage *)incrementalDecodedImageWithOptions:(SDImageCoderOptions *)options {
|
||||
UIImage *image;
|
||||
|
||||
if (_width + _height > 0) {
|
||||
// Create the image
|
||||
CGImageRef partialImageRef = CGImageSourceCreateImageAtIndex(_imageSource, 0, NULL);
|
||||
|
||||
if (partialImageRef) {
|
||||
CGFloat scale = _scale;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
#if SD_UIKIT || SD_WATCH
|
||||
image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:UIImageOrientationUp];
|
||||
#else
|
||||
image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:kCGImagePropertyOrientationUp];
|
||||
#endif
|
||||
CGImageRelease(partialImageRef);
|
||||
image.sd_imageFormat = SDImageFormatPNG;
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
#pragma mark - SDAnimatedImageCoder
|
||||
- (nullable instancetype)initWithAnimatedImageData:(nullable NSData *)data options:(nullable SDImageCoderOptions *)options {
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
self = [super init];
|
||||
if (self) {
|
||||
CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
||||
if (!imageSource) {
|
||||
return nil;
|
||||
}
|
||||
BOOL framesValid = [self scanAndCheckFramesValidWithImageSource:imageSource];
|
||||
if (!framesValid) {
|
||||
CFRelease(imageSource);
|
||||
return nil;
|
||||
}
|
||||
CGFloat scale = 1;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
_scale = scale;
|
||||
_imageSource = imageSource;
|
||||
_imageData = data;
|
||||
#if SD_UIKIT
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
||||
#endif
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)scanAndCheckFramesValidWithImageSource:(CGImageSourceRef)imageSource
|
||||
{
|
||||
if (!imageSource) {
|
||||
return NO;
|
||||
}
|
||||
NSUInteger frameCount = CGImageSourceGetCount(imageSource);
|
||||
NSUInteger loopCount = [self sd_imageLoopCountWithSource:imageSource];
|
||||
NSMutableArray<SDAPNGCoderFrame *> *frames = [NSMutableArray array];
|
||||
|
||||
for (size_t i = 0; i < frameCount; i++) {
|
||||
SDAPNGCoderFrame *frame = [[SDAPNGCoderFrame alloc] init];
|
||||
frame.index = i;
|
||||
frame.duration = [self sd_frameDurationAtIndex:i source:imageSource];
|
||||
[frames addObject:frame];
|
||||
}
|
||||
|
||||
_frameCount = frameCount;
|
||||
_loopCount = loopCount;
|
||||
_frames = [frames copy];
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSData *)animatedImageData
|
||||
{
|
||||
return _imageData;
|
||||
}
|
||||
|
||||
- (NSUInteger)animatedImageLoopCount
|
||||
{
|
||||
return _loopCount;
|
||||
}
|
||||
|
||||
- (NSUInteger)animatedImageFrameCount
|
||||
{
|
||||
return _frameCount;
|
||||
}
|
||||
|
||||
- (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index
|
||||
{
|
||||
if (index >= _frameCount) {
|
||||
return 0;
|
||||
}
|
||||
return _frames[index].duration;
|
||||
}
|
||||
|
||||
- (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index
|
||||
{
|
||||
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_imageSource, index, NULL);
|
||||
if (!imageRef) {
|
||||
return nil;
|
||||
}
|
||||
// Image/IO create CGImage does not decompressed, so we do this because this is called background queue, this can avoid main queue block when rendering(especially when one more imageViews use the same image instance)
|
||||
CGImageRef newImageRef = [SDImageCoderHelper CGImageCreateDecoded:imageRef];
|
||||
if (!newImageRef) {
|
||||
newImageRef = imageRef;
|
||||
} else {
|
||||
CGImageRelease(imageRef);
|
||||
}
|
||||
#if SD_MAC
|
||||
UIImage *image = [[UIImage alloc] initWithCGImage:newImageRef scale:_scale orientation:kCGImagePropertyOrientationUp];
|
||||
#else
|
||||
UIImage *image = [UIImage imageWithCGImage:newImageRef scale:_scale orientation:UIImageOrientationUp];
|
||||
#endif
|
||||
CGImageRelease(newImageRef);
|
||||
return image;
|
||||
+ (NSUInteger)defaultLoopCount {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -91,7 +91,7 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
|
|||
for (size_t i = 0; i < frameCount; i++) {
|
||||
@autoreleasepool {
|
||||
SDImageFrame *frame = frames[i];
|
||||
float frameDuration = frame.duration;
|
||||
NSTimeInterval frameDuration = frame.duration;
|
||||
CGImageRef frameImageRef = frame.image.CGImage;
|
||||
NSDictionary *frameProperties = @{(__bridge NSString *)kCGImagePropertyGIFDictionary : @{(__bridge NSString *)kCGImagePropertyGIFDelayTime : @(frameDuration)}};
|
||||
CGImageDestinationAddImage(imageDestination, frameImageRef, (__bridge CFDictionaryRef)frameProperties);
|
||||
|
@ -181,7 +181,7 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
|
|||
@autoreleasepool {
|
||||
// NSBitmapImageRep need to manually change frame. "Good taste" API
|
||||
[bitmapImageRep setProperty:NSImageCurrentFrame withValue:@(i)];
|
||||
float frameDuration = [[bitmapImageRep valueForProperty:NSImageCurrentFrameDuration] floatValue];
|
||||
NSTimeInterval frameDuration = [[bitmapImageRep valueForProperty:NSImageCurrentFrameDuration] doubleValue];
|
||||
NSImage *frameImage = [[NSImage alloc] initWithCGImage:bitmapImageRep.CGImage scale:scale orientation:kCGImagePropertyOrientationUp];
|
||||
SDImageFrame *frame = [SDImageFrame frameWithImage:frameImage duration:frameDuration];
|
||||
[frames addObject:frame];
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "SDImageCoder.h"
|
||||
#import "SDImageIOAnimatedCoder.h"
|
||||
|
||||
/**
|
||||
Built in coder using ImageIO that supports animated GIF encoding/decoding
|
||||
|
@ -15,7 +15,7 @@
|
|||
@note Use `SDImageGIFCoder` for fully animated GIFs. For `UIImageView`, it will produce animated `UIImage`(`NSImage` on macOS) for rendering. For `SDAnimatedImageView`, it will use `SDAnimatedImage` for rendering.
|
||||
@note The recommended approach for animated GIFs is using `SDAnimatedImage` with `SDAnimatedImageView`. It's more performant than `UIImageView` for GIF displaying(especially on memory usage)
|
||||
*/
|
||||
@interface SDImageGIFCoder : NSObject <SDProgressiveImageCoder, SDAnimatedImageCoder>
|
||||
@interface SDImageGIFCoder : SDImageIOAnimatedCoder <SDProgressiveImageCoder, SDAnimatedImageCoder>
|
||||
|
||||
@property (nonatomic, class, readonly, nonnull) SDImageGIFCoder *sharedCoder;
|
||||
|
||||
|
|
|
@ -7,53 +7,13 @@
|
|||
*/
|
||||
|
||||
#import "SDImageGIFCoder.h"
|
||||
#import "NSImage+Compatibility.h"
|
||||
#import "UIImage+Metadata.h"
|
||||
#import <ImageIO/ImageIO.h>
|
||||
#import "NSData+ImageContentType.h"
|
||||
#import "SDImageCoderHelper.h"
|
||||
#import "SDAnimatedImageRep.h"
|
||||
|
||||
@interface SDGIFCoderFrame : NSObject
|
||||
|
||||
@property (nonatomic, assign) NSUInteger index; // Frame index (zero based)
|
||||
@property (nonatomic, assign) NSTimeInterval duration; // Frame duration in seconds
|
||||
|
||||
@end
|
||||
|
||||
@implementation SDGIFCoderFrame
|
||||
@end
|
||||
|
||||
@implementation SDImageGIFCoder {
|
||||
size_t _width, _height;
|
||||
CGImageSourceRef _imageSource;
|
||||
NSData *_imageData;
|
||||
CGFloat _scale;
|
||||
NSUInteger _loopCount;
|
||||
NSUInteger _frameCount;
|
||||
NSArray<SDGIFCoderFrame *> *_frames;
|
||||
BOOL _finished;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
if (_imageSource) {
|
||||
CFRelease(_imageSource);
|
||||
_imageSource = NULL;
|
||||
}
|
||||
#if SD_UIKIT
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
||||
#if SD_MAC
|
||||
#import <CoreServices/CoreServices.h>
|
||||
#else
|
||||
#import <MobileCoreServices/MobileCoreServices.h>
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void)didReceiveMemoryWarning:(NSNotification *)notification
|
||||
{
|
||||
if (_imageSource) {
|
||||
for (size_t i = 0; i < _frameCount; i++) {
|
||||
CGImageSourceRemoveCacheAtIndex(_imageSource, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@implementation SDImageGIFCoder
|
||||
|
||||
+ (instancetype)sharedCoder {
|
||||
static SDImageGIFCoder *coder;
|
||||
|
@ -64,345 +24,34 @@
|
|||
return coder;
|
||||
}
|
||||
|
||||
#pragma mark - Decode
|
||||
- (BOOL)canDecodeFromData:(nullable NSData *)data {
|
||||
return ([NSData sd_imageFormatForImageData:data] == SDImageFormatGIF);
|
||||
#pragma mark - Subclass Override
|
||||
|
||||
+ (SDImageFormat)imageFormat {
|
||||
return SDImageFormatGIF;
|
||||
}
|
||||
|
||||
- (UIImage *)decodedImageWithData:(NSData *)data options:(nullable SDImageCoderOptions *)options {
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
CGFloat scale = 1;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
|
||||
#if SD_MAC
|
||||
SDAnimatedImageRep *imageRep = [[SDAnimatedImageRep alloc] initWithData:data];
|
||||
NSSize size = NSMakeSize(imageRep.pixelsWide / scale, imageRep.pixelsHigh / scale);
|
||||
imageRep.size = size;
|
||||
NSImage *animatedImage = [[NSImage alloc] initWithSize:size];
|
||||
[animatedImage addRepresentation:imageRep];
|
||||
return animatedImage;
|
||||
#else
|
||||
|
||||
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
||||
if (!source) {
|
||||
return nil;
|
||||
}
|
||||
size_t count = CGImageSourceGetCount(source);
|
||||
UIImage *animatedImage;
|
||||
|
||||
BOOL decodeFirstFrame = [options[SDImageCoderDecodeFirstFrameOnly] boolValue];
|
||||
if (decodeFirstFrame || count <= 1) {
|
||||
animatedImage = [[UIImage alloc] initWithData:data scale:scale];
|
||||
} else {
|
||||
NSMutableArray<SDImageFrame *> *frames = [NSMutableArray array];
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
|
||||
if (!imageRef) {
|
||||
continue;
|
||||
}
|
||||
|
||||
float duration = [self sd_frameDurationAtIndex:i source:source];
|
||||
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
|
||||
CGImageRelease(imageRef);
|
||||
|
||||
SDImageFrame *frame = [SDImageFrame frameWithImage:image duration:duration];
|
||||
[frames addObject:frame];
|
||||
}
|
||||
|
||||
NSUInteger loopCount = [self sd_imageLoopCountWithSource:source];
|
||||
|
||||
animatedImage = [SDImageCoderHelper animatedImageWithFrames:frames];
|
||||
animatedImage.sd_imageLoopCount = loopCount;
|
||||
}
|
||||
animatedImage.sd_imageFormat = SDImageFormatGIF;
|
||||
CFRelease(source);
|
||||
|
||||
return animatedImage;
|
||||
#endif
|
||||
+ (NSString *)imageUTType {
|
||||
return (__bridge NSString *)kUTTypeGIF;
|
||||
}
|
||||
|
||||
- (NSUInteger)sd_imageLoopCountWithSource:(CGImageSourceRef)source {
|
||||
NSUInteger loopCount = 1;
|
||||
NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(source, nil);
|
||||
NSDictionary *gifProperties = imageProperties[(__bridge NSString *)kCGImagePropertyGIFDictionary];
|
||||
if (gifProperties) {
|
||||
NSNumber *gifLoopCount = gifProperties[(__bridge NSString *)kCGImagePropertyGIFLoopCount];
|
||||
if (gifLoopCount != nil) {
|
||||
loopCount = gifLoopCount.unsignedIntegerValue;
|
||||
}
|
||||
}
|
||||
return loopCount;
|
||||
+ (NSString *)dictionaryProperty {
|
||||
return (__bridge NSString *)kCGImagePropertyGIFDictionary;
|
||||
}
|
||||
|
||||
- (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
|
||||
float frameDuration = 0.1f;
|
||||
CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
|
||||
if (!cfFrameProperties) {
|
||||
return frameDuration;
|
||||
}
|
||||
NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
|
||||
NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];
|
||||
|
||||
NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
|
||||
if (delayTimeUnclampedProp != nil) {
|
||||
frameDuration = [delayTimeUnclampedProp floatValue];
|
||||
} else {
|
||||
NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];
|
||||
if (delayTimeProp != nil) {
|
||||
frameDuration = [delayTimeProp floatValue];
|
||||
}
|
||||
}
|
||||
|
||||
// Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
|
||||
// We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
|
||||
// a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
|
||||
// for more information.
|
||||
|
||||
if (frameDuration < 0.011f) {
|
||||
frameDuration = 0.100f;
|
||||
}
|
||||
|
||||
CFRelease(cfFrameProperties);
|
||||
return frameDuration;
|
||||
+ (NSString *)unclampedDelayTimeProperty {
|
||||
return (__bridge NSString *)kCGImagePropertyGIFUnclampedDelayTime;
|
||||
}
|
||||
|
||||
#pragma mark - Progressive Decode
|
||||
|
||||
- (BOOL)canIncrementalDecodeFromData:(NSData *)data {
|
||||
return ([NSData sd_imageFormatForImageData:data] == SDImageFormatGIF);
|
||||
+ (NSString *)delayTimeProperty {
|
||||
return (__bridge NSString *)kCGImagePropertyGIFDelayTime;
|
||||
}
|
||||
|
||||
- (instancetype)initIncrementalWithOptions:(nullable SDImageCoderOptions *)options {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:SDImageFormatGIF];
|
||||
_imageSource = CGImageSourceCreateIncremental((__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageSourceTypeIdentifierHint : (__bridge NSString *)imageUTType});
|
||||
CGFloat scale = 1;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
_scale = scale;
|
||||
#if SD_UIKIT
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
||||
#endif
|
||||
}
|
||||
return self;
|
||||
+ (NSString *)loopCountProperty {
|
||||
return (__bridge NSString *)kCGImagePropertyGIFLoopCount;
|
||||
}
|
||||
|
||||
- (void)updateIncrementalData:(NSData *)data finished:(BOOL)finished {
|
||||
if (_finished) {
|
||||
return;
|
||||
}
|
||||
_imageData = data;
|
||||
_finished = finished;
|
||||
|
||||
// The following code is from http://www.cocoaintheshell.com/2011/05/progressive-images-download-imageio/
|
||||
// Thanks to the author @Nyx0uf
|
||||
|
||||
// Update the data source, we must pass ALL the data, not just the new bytes
|
||||
CGImageSourceUpdateData(_imageSource, (__bridge CFDataRef)data, finished);
|
||||
|
||||
if (_width + _height == 0) {
|
||||
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(_imageSource, 0, NULL);
|
||||
if (properties) {
|
||||
CFTypeRef val = CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight);
|
||||
if (val) CFNumberGetValue(val, kCFNumberLongType, &_height);
|
||||
val = CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth);
|
||||
if (val) CFNumberGetValue(val, kCFNumberLongType, &_width);
|
||||
CFRelease(properties);
|
||||
}
|
||||
}
|
||||
|
||||
// For animated image progressive decoding because the frame count and duration may be changed.
|
||||
[self scanAndCheckFramesValidWithImageSource:_imageSource];
|
||||
}
|
||||
|
||||
- (UIImage *)incrementalDecodedImageWithOptions:(SDImageCoderOptions *)options {
|
||||
UIImage *image;
|
||||
|
||||
if (_width + _height > 0) {
|
||||
// Create the image
|
||||
CGImageRef partialImageRef = CGImageSourceCreateImageAtIndex(_imageSource, 0, NULL);
|
||||
|
||||
if (partialImageRef) {
|
||||
CGFloat scale = _scale;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
#if SD_UIKIT || SD_WATCH
|
||||
image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:UIImageOrientationUp];
|
||||
#else
|
||||
image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:kCGImagePropertyOrientationUp];
|
||||
#endif
|
||||
CGImageRelease(partialImageRef);
|
||||
image.sd_imageFormat = SDImageFormatGIF;
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
#pragma mark - Encode
|
||||
- (BOOL)canEncodeToFormat:(SDImageFormat)format {
|
||||
return (format == SDImageFormatGIF);
|
||||
}
|
||||
|
||||
- (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format options:(nullable SDImageCoderOptions *)options {
|
||||
if (!image) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (format != SDImageFormatGIF) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSMutableData *imageData = [NSMutableData data];
|
||||
CFStringRef imageUTType = [NSData sd_UTTypeFromImageFormat:SDImageFormatGIF];
|
||||
NSArray<SDImageFrame *> *frames = [SDImageCoderHelper framesFromAnimatedImage:image];
|
||||
|
||||
// Create an image destination. GIF does not support EXIF image orientation
|
||||
// The `CGImageDestinationCreateWithData` will log a warning when count is 0, use 1 instead.
|
||||
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageData, imageUTType, frames.count ?: 1, NULL);
|
||||
if (!imageDestination) {
|
||||
// Handle failure.
|
||||
return nil;
|
||||
}
|
||||
NSMutableDictionary *properties = [NSMutableDictionary dictionary];
|
||||
double compressionQuality = 1;
|
||||
if (options[SDImageCoderEncodeCompressionQuality]) {
|
||||
compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue];
|
||||
}
|
||||
properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = @(compressionQuality);
|
||||
|
||||
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];
|
||||
if (encodeFirstFrame || frames.count == 0) {
|
||||
// for static single GIF images
|
||||
CGImageDestinationAddImage(imageDestination, image.CGImage, (__bridge CFDictionaryRef)properties);
|
||||
} else {
|
||||
// for animated GIF images
|
||||
NSUInteger loopCount = image.sd_imageLoopCount;
|
||||
NSDictionary *gifProperties = @{(__bridge NSString *)kCGImagePropertyGIFLoopCount : @(loopCount)};
|
||||
properties[(__bridge NSString *)kCGImagePropertyGIFDictionary] = gifProperties;
|
||||
CGImageDestinationSetProperties(imageDestination, (__bridge CFDictionaryRef)properties);
|
||||
|
||||
for (size_t i = 0; i < frames.count; i++) {
|
||||
SDImageFrame *frame = frames[i];
|
||||
float frameDuration = frame.duration;
|
||||
CGImageRef frameImageRef = frame.image.CGImage;
|
||||
NSDictionary *frameProperties = @{(__bridge NSString *)kCGImagePropertyGIFDictionary : @{(__bridge NSString *)kCGImagePropertyGIFDelayTime : @(frameDuration)}};
|
||||
CGImageDestinationAddImage(imageDestination, frameImageRef, (__bridge CFDictionaryRef)frameProperties);
|
||||
}
|
||||
}
|
||||
// Finalize the destination.
|
||||
if (CGImageDestinationFinalize(imageDestination) == NO) {
|
||||
// Handle failure.
|
||||
imageData = nil;
|
||||
}
|
||||
|
||||
CFRelease(imageDestination);
|
||||
|
||||
return [imageData copy];
|
||||
}
|
||||
|
||||
#pragma mark - SDAnimatedImageCoder
|
||||
- (nullable instancetype)initWithAnimatedImageData:(nullable NSData *)data options:(nullable SDImageCoderOptions *)options {
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
self = [super init];
|
||||
if (self) {
|
||||
CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
||||
if (!imageSource) {
|
||||
return nil;
|
||||
}
|
||||
BOOL framesValid = [self scanAndCheckFramesValidWithImageSource:imageSource];
|
||||
if (!framesValid) {
|
||||
CFRelease(imageSource);
|
||||
return nil;
|
||||
}
|
||||
CGFloat scale = 1;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
_scale = scale;
|
||||
_imageSource = imageSource;
|
||||
_imageData = data;
|
||||
#if SD_UIKIT
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
||||
#endif
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)scanAndCheckFramesValidWithImageSource:(CGImageSourceRef)imageSource {
|
||||
if (!imageSource) {
|
||||
return NO;
|
||||
}
|
||||
NSUInteger frameCount = CGImageSourceGetCount(imageSource);
|
||||
NSUInteger loopCount = [self sd_imageLoopCountWithSource:imageSource];
|
||||
NSMutableArray<SDGIFCoderFrame *> *frames = [NSMutableArray array];
|
||||
|
||||
for (size_t i = 0; i < frameCount; i++) {
|
||||
SDGIFCoderFrame *frame = [[SDGIFCoderFrame alloc] init];
|
||||
frame.index = i;
|
||||
frame.duration = [self sd_frameDurationAtIndex:i source:imageSource];
|
||||
[frames addObject:frame];
|
||||
}
|
||||
|
||||
_frameCount = frameCount;
|
||||
_loopCount = loopCount;
|
||||
_frames = [frames copy];
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSData *)animatedImageData {
|
||||
return _imageData;
|
||||
}
|
||||
|
||||
- (NSUInteger)animatedImageLoopCount {
|
||||
return _loopCount;
|
||||
}
|
||||
|
||||
- (NSUInteger)animatedImageFrameCount {
|
||||
return _frameCount;
|
||||
}
|
||||
|
||||
- (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index {
|
||||
if (index >= _frameCount) {
|
||||
return 0;
|
||||
}
|
||||
return _frames[index].duration;
|
||||
}
|
||||
|
||||
- (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index {
|
||||
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_imageSource, index, NULL);
|
||||
if (!imageRef) {
|
||||
return nil;
|
||||
}
|
||||
// Image/IO create CGImage does not decode, so we do this because this is called background queue, this can avoid main queue block when rendering(especially when one more imageViews use the same image instance)
|
||||
CGImageRef newImageRef = [SDImageCoderHelper CGImageCreateDecoded:imageRef];
|
||||
if (!newImageRef) {
|
||||
newImageRef = imageRef;
|
||||
} else {
|
||||
CGImageRelease(imageRef);
|
||||
}
|
||||
#if SD_MAC
|
||||
UIImage *image = [[UIImage alloc] initWithCGImage:newImageRef scale:_scale orientation:kCGImagePropertyOrientationUp];
|
||||
#else
|
||||
UIImage *image = [[UIImage alloc] initWithCGImage:newImageRef scale:_scale orientation:UIImageOrientationUp];
|
||||
#endif
|
||||
CGImageRelease(newImageRef);
|
||||
return image;
|
||||
+ (NSUInteger)defaultLoopCount {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* This file is part of the SDWebImage package.
|
||||
* (c) Olivier Poitrey <rs@dailymotion.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <ImageIO/ImageIO.h>
|
||||
#import "SDImageCoder.h"
|
||||
|
||||
/**
|
||||
This is the abstract class for all animated coder, which use the Image/IO API. You can not use this directly as real coders. A exception will be raised if you use this class.
|
||||
All of the properties need the subclass to implment and works as expceted.
|
||||
For Image/IO, See Apple's documentation: https://developer.apple.com/documentation/imageio
|
||||
*/
|
||||
@interface SDImageIOAnimatedCoder : NSObject <SDProgressiveImageCoder, SDAnimatedImageCoder>
|
||||
|
||||
#pragma mark - Subclass Override
|
||||
/**
|
||||
The supported animated image format. Such as `SDImageFormatGIF`.
|
||||
@note Subclass override.
|
||||
*/
|
||||
@property (class, readonly) SDImageFormat imageFormat;
|
||||
/**
|
||||
The supported image format UTI Type. Such as `kUTTypeGIF`.
|
||||
@note Subclass override.
|
||||
*/
|
||||
@property (class, readonly, nonnull) NSString *imageUTType;
|
||||
/**
|
||||
The image container property key used in Image/IO API. Such as `kCGImagePropertyGIFDictionary`.
|
||||
@note Subclass override.
|
||||
*/
|
||||
@property (class, readonly, nonnull) NSString *dictionaryProperty;
|
||||
/**
|
||||
The image unclamped deply time property key used in Image/IO API. Such as `kCGImagePropertyGIFUnclampedDelayTime`
|
||||
@note Subclass override.
|
||||
*/
|
||||
@property (class, readonly, nonnull) NSString *unclampedDelayTimeProperty;
|
||||
/**
|
||||
The image delay time property key used in Image/IO API. Such as `kCGImagePropertyGIFDelayTime`.
|
||||
@note Subclass override.
|
||||
*/
|
||||
@property (class, readonly, nonnull) NSString *delayTimeProperty;
|
||||
/**
|
||||
The image loop count property key used in Image/IO API. Such as `kCGImagePropertyGIFLoopCount`.
|
||||
@note Subclass override.
|
||||
*/
|
||||
@property (class, readonly, nonnull) NSString *loopCountProperty;
|
||||
/**
|
||||
The default loop count when there are no any loop count information inside image container metadata.
|
||||
For example, for GIF format, the standard use 1 (treat as static image). For APNG format, the standard use 0 (infinity loop).
|
||||
@note Subclass override.
|
||||
*/
|
||||
@property (class, readonly) NSUInteger defaultLoopCount;
|
||||
|
||||
@end
|
|
@ -0,0 +1,445 @@
|
|||
/*
|
||||
* This file is part of the SDWebImage package.
|
||||
* (c) Olivier Poitrey <rs@dailymotion.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
#import "SDImageIOAnimatedCoder.h"
|
||||
#import "NSImage+Compatibility.h"
|
||||
#import "UIImage+Metadata.h"
|
||||
#import "NSData+ImageContentType.h"
|
||||
#import "SDImageCoderHelper.h"
|
||||
#import "SDAnimatedImageRep.h"
|
||||
|
||||
@interface SDImageIOCoderFrame : NSObject
|
||||
|
||||
@property (nonatomic, assign) NSUInteger index; // Frame index (zero based)
|
||||
@property (nonatomic, assign) NSTimeInterval duration; // Frame duration in seconds
|
||||
|
||||
@end
|
||||
|
||||
@implementation SDImageIOCoderFrame
|
||||
@end
|
||||
|
||||
@implementation SDImageIOAnimatedCoder {
|
||||
size_t _width, _height;
|
||||
CGImageSourceRef _imageSource;
|
||||
NSData *_imageData;
|
||||
CGFloat _scale;
|
||||
NSUInteger _loopCount;
|
||||
NSUInteger _frameCount;
|
||||
NSArray<SDImageIOCoderFrame *> *_frames;
|
||||
BOOL _finished;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
if (_imageSource) {
|
||||
CFRelease(_imageSource);
|
||||
_imageSource = NULL;
|
||||
}
|
||||
#if SD_UIKIT
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void)didReceiveMemoryWarning:(NSNotification *)notification
|
||||
{
|
||||
if (_imageSource) {
|
||||
for (size_t i = 0; i < _frameCount; i++) {
|
||||
CGImageSourceRemoveCacheAtIndex(_imageSource, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Subclass Override
|
||||
|
||||
+ (SDImageFormat)imageFormat {
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"Must override %@ in subclass", NSStringFromSelector(_cmd)]
|
||||
userInfo:nil];
|
||||
}
|
||||
|
||||
+ (NSString *)imageUTType {
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"Must override %@ in subclass", NSStringFromSelector(_cmd)]
|
||||
userInfo:nil];
|
||||
}
|
||||
|
||||
+ (NSString *)dictionaryProperty {
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"Must override %@ in subclass", NSStringFromSelector(_cmd)]
|
||||
userInfo:nil];
|
||||
}
|
||||
|
||||
+ (NSString *)unclampedDelayTimeProperty {
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"Must override %@ in subclass", NSStringFromSelector(_cmd)]
|
||||
userInfo:nil];
|
||||
}
|
||||
|
||||
+ (NSString *)delayTimeProperty {
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"Must override %@ in subclass", NSStringFromSelector(_cmd)]
|
||||
userInfo:nil];
|
||||
}
|
||||
|
||||
+ (NSString *)loopCountProperty {
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"Must override %@ in subclass", NSStringFromSelector(_cmd)]
|
||||
userInfo:nil];
|
||||
}
|
||||
|
||||
+ (NSUInteger)defaultLoopCount {
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"Must override %@ in subclass", NSStringFromSelector(_cmd)]
|
||||
userInfo:nil];
|
||||
}
|
||||
|
||||
#pragma mark - Utils
|
||||
|
||||
+ (NSUInteger)imageLoopCountWithSource:(CGImageSourceRef)source {
|
||||
NSUInteger loopCount = 1;
|
||||
NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(source, nil);
|
||||
NSDictionary *containerProperties = imageProperties[self.dictionaryProperty];
|
||||
if (containerProperties) {
|
||||
NSNumber *containerLoopCount = containerProperties[self.loopCountProperty];
|
||||
if (containerLoopCount != nil) {
|
||||
loopCount = containerLoopCount.unsignedIntegerValue;
|
||||
}
|
||||
}
|
||||
return loopCount;
|
||||
}
|
||||
|
||||
+ (NSTimeInterval)frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
|
||||
NSTimeInterval frameDuration = 0.1;
|
||||
CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
|
||||
if (!cfFrameProperties) {
|
||||
return frameDuration;
|
||||
}
|
||||
NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
|
||||
NSDictionary *containerProperties = frameProperties[self.dictionaryProperty];
|
||||
|
||||
NSNumber *delayTimeUnclampedProp = containerProperties[self.unclampedDelayTimeProperty];
|
||||
if (delayTimeUnclampedProp != nil) {
|
||||
frameDuration = [delayTimeUnclampedProp doubleValue];
|
||||
} else {
|
||||
NSNumber *delayTimeProp = containerProperties[self.delayTimeProperty];
|
||||
if (delayTimeProp != nil) {
|
||||
frameDuration = [delayTimeProp doubleValue];
|
||||
}
|
||||
}
|
||||
|
||||
// Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
|
||||
// We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
|
||||
// a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
|
||||
// for more information.
|
||||
|
||||
if (frameDuration < 0.011) {
|
||||
frameDuration = 0.1;
|
||||
}
|
||||
|
||||
CFRelease(cfFrameProperties);
|
||||
return frameDuration;
|
||||
}
|
||||
|
||||
#pragma mark - Decode
|
||||
- (BOOL)canDecodeFromData:(nullable NSData *)data {
|
||||
return ([NSData sd_imageFormatForImageData:data] == self.class.imageFormat);
|
||||
}
|
||||
|
||||
- (UIImage *)decodedImageWithData:(NSData *)data options:(nullable SDImageCoderOptions *)options {
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
CGFloat scale = 1;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
|
||||
#if SD_MAC
|
||||
SDAnimatedImageRep *imageRep = [[SDAnimatedImageRep alloc] initWithData:data];
|
||||
NSSize size = NSMakeSize(imageRep.pixelsWide / scale, imageRep.pixelsHigh / scale);
|
||||
imageRep.size = size;
|
||||
NSImage *animatedImage = [[NSImage alloc] initWithSize:size];
|
||||
[animatedImage addRepresentation:imageRep];
|
||||
return animatedImage;
|
||||
#else
|
||||
|
||||
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
||||
if (!source) {
|
||||
return nil;
|
||||
}
|
||||
size_t count = CGImageSourceGetCount(source);
|
||||
UIImage *animatedImage;
|
||||
|
||||
BOOL decodeFirstFrame = [options[SDImageCoderDecodeFirstFrameOnly] boolValue];
|
||||
if (decodeFirstFrame || count <= 1) {
|
||||
animatedImage = [[UIImage alloc] initWithData:data scale:scale];
|
||||
} else {
|
||||
NSMutableArray<SDImageFrame *> *frames = [NSMutableArray array];
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
|
||||
if (!imageRef) {
|
||||
continue;
|
||||
}
|
||||
|
||||
NSTimeInterval duration = [self.class frameDurationAtIndex:i source:source];
|
||||
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
|
||||
CGImageRelease(imageRef);
|
||||
|
||||
SDImageFrame *frame = [SDImageFrame frameWithImage:image duration:duration];
|
||||
[frames addObject:frame];
|
||||
}
|
||||
|
||||
NSUInteger loopCount = [self.class imageLoopCountWithSource:source];
|
||||
|
||||
animatedImage = [SDImageCoderHelper animatedImageWithFrames:frames];
|
||||
animatedImage.sd_imageLoopCount = loopCount;
|
||||
}
|
||||
animatedImage.sd_imageFormat = self.class.imageFormat;
|
||||
CFRelease(source);
|
||||
|
||||
return animatedImage;
|
||||
#endif
|
||||
}
|
||||
|
||||
#pragma mark - Progressive Decode
|
||||
|
||||
- (BOOL)canIncrementalDecodeFromData:(NSData *)data {
|
||||
return ([NSData sd_imageFormatForImageData:data] == self.class.imageFormat);
|
||||
}
|
||||
|
||||
- (instancetype)initIncrementalWithOptions:(nullable SDImageCoderOptions *)options {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
NSString *imageUTType = self.class.imageUTType;
|
||||
_imageSource = CGImageSourceCreateIncremental((__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageSourceTypeIdentifierHint : imageUTType});
|
||||
CGFloat scale = 1;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
_scale = scale;
|
||||
#if SD_UIKIT
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
||||
#endif
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)updateIncrementalData:(NSData *)data finished:(BOOL)finished {
|
||||
if (_finished) {
|
||||
return;
|
||||
}
|
||||
_imageData = data;
|
||||
_finished = finished;
|
||||
|
||||
// The following code is from http://www.cocoaintheshell.com/2011/05/progressive-images-download-imageio/
|
||||
// Thanks to the author @Nyx0uf
|
||||
|
||||
// Update the data source, we must pass ALL the data, not just the new bytes
|
||||
CGImageSourceUpdateData(_imageSource, (__bridge CFDataRef)data, finished);
|
||||
|
||||
if (_width + _height == 0) {
|
||||
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(_imageSource, 0, NULL);
|
||||
if (properties) {
|
||||
CFTypeRef val = CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight);
|
||||
if (val) CFNumberGetValue(val, kCFNumberLongType, &_height);
|
||||
val = CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth);
|
||||
if (val) CFNumberGetValue(val, kCFNumberLongType, &_width);
|
||||
CFRelease(properties);
|
||||
}
|
||||
}
|
||||
|
||||
// For animated image progressive decoding because the frame count and duration may be changed.
|
||||
[self scanAndCheckFramesValidWithImageSource:_imageSource];
|
||||
}
|
||||
|
||||
- (UIImage *)incrementalDecodedImageWithOptions:(SDImageCoderOptions *)options {
|
||||
UIImage *image;
|
||||
|
||||
if (_width + _height > 0) {
|
||||
// Create the image
|
||||
CGImageRef partialImageRef = CGImageSourceCreateImageAtIndex(_imageSource, 0, NULL);
|
||||
|
||||
if (partialImageRef) {
|
||||
CGFloat scale = _scale;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
#if SD_UIKIT || SD_WATCH
|
||||
image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:UIImageOrientationUp];
|
||||
#else
|
||||
image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:kCGImagePropertyOrientationUp];
|
||||
#endif
|
||||
CGImageRelease(partialImageRef);
|
||||
image.sd_imageFormat = self.class.imageFormat;
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
#pragma mark - Encode
|
||||
- (BOOL)canEncodeToFormat:(SDImageFormat)format {
|
||||
return (format == self.class.imageFormat);
|
||||
}
|
||||
|
||||
- (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format options:(nullable SDImageCoderOptions *)options {
|
||||
if (!image) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (format != self.class.imageFormat) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSMutableData *imageData = [NSMutableData data];
|
||||
NSString *imageUTType = self.class.imageUTType;
|
||||
NSArray<SDImageFrame *> *frames = [SDImageCoderHelper framesFromAnimatedImage:image];
|
||||
|
||||
// Create an image destination. Animated Image does not support EXIF image orientation TODO
|
||||
// The `CGImageDestinationCreateWithData` will log a warning when count is 0, use 1 instead.
|
||||
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageData, (__bridge CFStringRef)imageUTType, frames.count ?: 1, NULL);
|
||||
if (!imageDestination) {
|
||||
// Handle failure.
|
||||
return nil;
|
||||
}
|
||||
NSMutableDictionary *properties = [NSMutableDictionary dictionary];
|
||||
double compressionQuality = 1;
|
||||
if (options[SDImageCoderEncodeCompressionQuality]) {
|
||||
compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue];
|
||||
}
|
||||
properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = @(compressionQuality);
|
||||
|
||||
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];
|
||||
if (encodeFirstFrame || frames.count == 0) {
|
||||
// for static single images
|
||||
CGImageDestinationAddImage(imageDestination, image.CGImage, (__bridge CFDictionaryRef)properties);
|
||||
} else {
|
||||
// for animated images
|
||||
NSUInteger loopCount = image.sd_imageLoopCount;
|
||||
NSDictionary *containerProperties = @{self.class.loopCountProperty : @(loopCount)};
|
||||
properties[self.class.dictionaryProperty] = containerProperties;
|
||||
CGImageDestinationSetProperties(imageDestination, (__bridge CFDictionaryRef)properties);
|
||||
|
||||
for (size_t i = 0; i < frames.count; i++) {
|
||||
SDImageFrame *frame = frames[i];
|
||||
NSTimeInterval frameDuration = frame.duration;
|
||||
CGImageRef frameImageRef = frame.image.CGImage;
|
||||
NSDictionary *frameProperties = @{self.class.dictionaryProperty : @{self.class.delayTimeProperty : @(frameDuration)}};
|
||||
CGImageDestinationAddImage(imageDestination, frameImageRef, (__bridge CFDictionaryRef)frameProperties);
|
||||
}
|
||||
}
|
||||
// Finalize the destination.
|
||||
if (CGImageDestinationFinalize(imageDestination) == NO) {
|
||||
// Handle failure.
|
||||
imageData = nil;
|
||||
}
|
||||
|
||||
CFRelease(imageDestination);
|
||||
|
||||
return [imageData copy];
|
||||
}
|
||||
|
||||
#pragma mark - SDAnimatedImageCoder
|
||||
- (nullable instancetype)initWithAnimatedImageData:(nullable NSData *)data options:(nullable SDImageCoderOptions *)options {
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
self = [super init];
|
||||
if (self) {
|
||||
CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
||||
if (!imageSource) {
|
||||
return nil;
|
||||
}
|
||||
BOOL framesValid = [self scanAndCheckFramesValidWithImageSource:imageSource];
|
||||
if (!framesValid) {
|
||||
CFRelease(imageSource);
|
||||
return nil;
|
||||
}
|
||||
CGFloat scale = 1;
|
||||
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
|
||||
if (scaleFactor != nil) {
|
||||
scale = MAX([scaleFactor doubleValue], 1);
|
||||
}
|
||||
_scale = scale;
|
||||
_imageSource = imageSource;
|
||||
_imageData = data;
|
||||
#if SD_UIKIT
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
||||
#endif
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)scanAndCheckFramesValidWithImageSource:(CGImageSourceRef)imageSource {
|
||||
if (!imageSource) {
|
||||
return NO;
|
||||
}
|
||||
NSUInteger frameCount = CGImageSourceGetCount(imageSource);
|
||||
NSUInteger loopCount = [self.class imageLoopCountWithSource:imageSource];
|
||||
NSMutableArray<SDImageIOCoderFrame *> *frames = [NSMutableArray array];
|
||||
|
||||
for (size_t i = 0; i < frameCount; i++) {
|
||||
SDImageIOCoderFrame *frame = [[SDImageIOCoderFrame alloc] init];
|
||||
frame.index = i;
|
||||
frame.duration = [self.class frameDurationAtIndex:i source:imageSource];
|
||||
[frames addObject:frame];
|
||||
}
|
||||
|
||||
_frameCount = frameCount;
|
||||
_loopCount = loopCount;
|
||||
_frames = [frames copy];
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSData *)animatedImageData {
|
||||
return _imageData;
|
||||
}
|
||||
|
||||
- (NSUInteger)animatedImageLoopCount {
|
||||
return _loopCount;
|
||||
}
|
||||
|
||||
- (NSUInteger)animatedImageFrameCount {
|
||||
return _frameCount;
|
||||
}
|
||||
|
||||
- (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index {
|
||||
if (index >= _frameCount) {
|
||||
return 0;
|
||||
}
|
||||
return _frames[index].duration;
|
||||
}
|
||||
|
||||
- (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index {
|
||||
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_imageSource, index, NULL);
|
||||
if (!imageRef) {
|
||||
return nil;
|
||||
}
|
||||
// Image/IO create CGImage does not decode, so we do this because this is called background queue, this can avoid main queue block when rendering(especially when one more imageViews use the same image instance)
|
||||
CGImageRef newImageRef = [SDImageCoderHelper CGImageCreateDecoded:imageRef];
|
||||
if (!newImageRef) {
|
||||
newImageRef = imageRef;
|
||||
} else {
|
||||
CGImageRelease(imageRef);
|
||||
}
|
||||
#if SD_MAC
|
||||
UIImage *image = [[UIImage alloc] initWithCGImage:newImageRef scale:_scale orientation:kCGImagePropertyOrientationUp];
|
||||
#else
|
||||
UIImage *image = [[UIImage alloc] initWithCGImage:newImageRef scale:_scale orientation:UIImageOrientationUp];
|
||||
#endif
|
||||
CGImageRelease(newImageRef);
|
||||
return image;
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
/*
|
||||
* This file is part of the SDWebImage package.
|
||||
* (c) Olivier Poitrey <rs@dailymotion.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
#import "SDWebImageCompat.h"
|
||||
#import "SDImageAPNGCoder.h"
|
||||
|
||||
@interface SDImageAPNGCoder ()
|
||||
|
||||
- (float)sd_frameDurationAtIndex:(NSUInteger)index source:(nonnull CGImageSourceRef)source;
|
||||
- (NSUInteger)sd_imageLoopCountWithSource:(nonnull CGImageSourceRef)source;
|
||||
|
||||
@end
|
|
@ -1,16 +0,0 @@
|
|||
/*
|
||||
* This file is part of the SDWebImage package.
|
||||
* (c) Olivier Poitrey <rs@dailymotion.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
#import "SDWebImageCompat.h"
|
||||
#import "SDImageGIFCoder.h"
|
||||
|
||||
@interface SDImageGIFCoder ()
|
||||
|
||||
- (float)sd_frameDurationAtIndex:(NSUInteger)index source:(nonnull CGImageSourceRef)source;
|
||||
|
||||
@end
|
|
@ -0,0 +1,17 @@
|
|||
/*
|
||||
* This file is part of the SDWebImage package.
|
||||
* (c) Olivier Poitrey <rs@dailymotion.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "SDImageIOAnimatedCoder.h"
|
||||
|
||||
@interface SDImageIOAnimatedCoder ()
|
||||
|
||||
+ (NSTimeInterval)frameDurationAtIndex:(NSUInteger)index source:(nonnull CGImageSourceRef)source;
|
||||
+ (NSUInteger)imageLoopCountWithSource:(nonnull CGImageSourceRef)source;
|
||||
|
||||
@end
|
|
@ -67,6 +67,7 @@ FOUNDATION_EXPORT const unsigned char WebImageVersionString[];
|
|||
#import <SDWebImage/SDWebImageDefine.h>
|
||||
#import <SDWebImage/SDWebImageError.h>
|
||||
#import <SDWebImage/SDWebImageOptionsProcessor.h>
|
||||
#import <SDWebImage/SDImageIOAnimatedCoder.h>
|
||||
|
||||
// Mac
|
||||
#if __has_include(<SDWebImage/NSImage+Compatibility.h>)
|
||||
|
|
Loading…
Reference in New Issue