@@ -19,6 +19,8 @@ static inline SDImageFormat SDImageFormatFromYYImageType(YYImageType type) {
19
19
return SDImageFormatGIF;
20
20
case YYImageTypeTIFF:
21
21
return SDImageFormatTIFF;
22
+ case YYImageTypeBMP:
23
+ return SDImageFormatBMP;
22
24
case YYImageTypeWebP:
23
25
return SDImageFormatWebP;
24
26
default :
@@ -57,6 +59,10 @@ - (BOOL)isAllFramesLoaded {
57
59
return self.preloadAllAnimatedImageFrames ;
58
60
}
59
61
62
+ - (SDImageFormat)animatedImageFormat {
63
+ return SDImageFormatFromYYImageType (self.animatedImageType );
64
+ }
65
+
60
66
@end
61
67
62
68
@implementation YYImage (MemoryCacheCost)
@@ -86,7 +92,7 @@ - (NSUInteger)sd_memoryCost {
86
92
@implementation YYImage (Metadata)
87
93
88
94
- (BOOL )sd_isAnimated {
89
- return YES ;
95
+ return self. animatedImageFrameCount > 1 ;
90
96
}
91
97
92
98
- (NSUInteger )sd_imageLoopCount {
@@ -97,8 +103,22 @@ - (void)setSd_imageLoopCount:(NSUInteger)sd_imageLoopCount {
97
103
return ;
98
104
}
99
105
106
+ - (NSUInteger )sd_imageFrameCount {
107
+ NSUInteger frameCount = self.animatedImageFrameCount ;
108
+ if (frameCount > 1 ) {
109
+ return frameCount;
110
+ } else {
111
+ return 1 ;
112
+ }
113
+ }
114
+
100
115
- (SDImageFormat)sd_imageFormat {
101
- return SDImageFormatFromYYImageType (self.animatedImageType );
116
+ NSData *animatedImageData = self.animatedImageData ;
117
+ if (animatedImageData) {
118
+ return self.animatedImageFormat ;
119
+ } else {
120
+ return [super sd_imageFormat ];
121
+ }
102
122
}
103
123
104
124
- (void )setSd_imageFormat : (SDImageFormat)sd_imageFormat {
@@ -110,3 +130,69 @@ - (BOOL)sd_isVector {
110
130
}
111
131
112
132
@end
133
+
134
+ @implementation YYImage (MultiFormat)
135
+
136
+ + (nullable UIImage *)sd_imageWithData : (nullable NSData *)data {
137
+ return [self sd_imageWithData: data scale: 1 ];
138
+ }
139
+
140
+ + (nullable UIImage *)sd_imageWithData : (nullable NSData *)data scale : (CGFloat )scale {
141
+ return [self sd_imageWithData: data scale: scale firstFrameOnly: NO ];
142
+ }
143
+
144
+ + (nullable UIImage *)sd_imageWithData : (nullable NSData *)data scale : (CGFloat )scale firstFrameOnly : (BOOL )firstFrameOnly {
145
+ if (!data) {
146
+ return nil ;
147
+ }
148
+ return [[self alloc ] initWithData: data scale: scale options: @{SDImageCoderDecodeFirstFrameOnly : @(firstFrameOnly)}];
149
+ }
150
+
151
+ - (nullable NSData *)sd_imageData {
152
+ NSData *imageData = self.animatedImageData ;
153
+ if (imageData) {
154
+ return imageData;
155
+ } else {
156
+ return [self sd_imageDataAsFormat: self .animatedImageFormat];
157
+ }
158
+ }
159
+
160
+ - (nullable NSData *)sd_imageDataAsFormat : (SDImageFormat)imageFormat {
161
+ return [self sd_imageDataAsFormat: imageFormat compressionQuality: 1 ];
162
+ }
163
+
164
+ - (nullable NSData *)sd_imageDataAsFormat : (SDImageFormat)imageFormat compressionQuality : (double )compressionQuality {
165
+ return [self sd_imageDataAsFormat: imageFormat compressionQuality: compressionQuality firstFrameOnly: NO ];
166
+ }
167
+
168
+ - (nullable NSData *)sd_imageDataAsFormat : (SDImageFormat)imageFormat compressionQuality : (double )compressionQuality firstFrameOnly : (BOOL )firstFrameOnly {
169
+ // Protect when user input the imageFormat == self.animatedImageFormat && compressionQuality == 1
170
+ // This should be treated as grabbing `self.animatedImageData` as well :)
171
+ NSData *imageData;
172
+ if (imageFormat == self.animatedImageFormat && compressionQuality == 1 ) {
173
+ imageData = self.animatedImageData ;
174
+ }
175
+ if (imageData) return imageData;
176
+
177
+ SDImageCoderOptions *options = @{SDImageCoderEncodeCompressionQuality : @(compressionQuality), SDImageCoderEncodeFirstFrameOnly : @(firstFrameOnly)};
178
+ NSUInteger frameCount = self.animatedImageFrameCount ;
179
+ if (frameCount <= 1 ) {
180
+ // Static image
181
+ imageData = [SDImageCodersManager.sharedManager encodedDataWithImage: self format: imageFormat options: options];
182
+ }
183
+ if (imageData) return imageData;
184
+
185
+ NSUInteger loopCount = self.animatedImageLoopCount ;
186
+ // Keep animated image encoding, loop each frame.
187
+ NSMutableArray <SDImageFrame *> *frames = [NSMutableArray arrayWithCapacity: frameCount];
188
+ for (size_t i = 0 ; i < frameCount; i++) {
189
+ UIImage *image = [self animatedImageFrameAtIndex: i];
190
+ NSTimeInterval duration = [self animatedImageDurationAtIndex: i];
191
+ SDImageFrame *frame = [SDImageFrame frameWithImage: image duration: duration];
192
+ [frames addObject: frame];
193
+ }
194
+ imageData = [SDImageCodersManager.sharedManager encodedDataWithFrames: frames loopCount: loopCount format: imageFormat options: options];
195
+ return imageData;
196
+ }
197
+
198
+ @end
0 commit comments