Skip to content

Commit 39dcfd8

Browse files
committed
Add SDImageYYCoder class, to support treat YYImageDecoder as custom coder class
1 parent c48f5b4 commit 39dcfd8

File tree

5 files changed

+216
-53
lines changed

5 files changed

+216
-53
lines changed

Example/Podfile.lock

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,25 @@ PODS:
1414
- YYImage/Core (1.0.4)
1515

1616
DEPENDENCIES:
17-
- SDWebImage/Core (from `[email protected]:rs/SDWebImage.git`, branch `5.x`)
17+
- "SDWebImage/Core (from `[email protected]:rs/SDWebImage.git`, branch `5.x`)"
1818
- SDWebImageYYPlugin (from `../`)
1919

20+
SPEC REPOS:
21+
https://github.com/cocoapods/specs.git:
22+
- YYCache
23+
- YYImage
24+
2025
EXTERNAL SOURCES:
2126
SDWebImage:
2227
:branch: 5.x
23-
:git: [email protected]:rs/SDWebImage.git
28+
:git: "[email protected]:rs/SDWebImage.git"
2429
SDWebImageYYPlugin:
25-
:path: ../
30+
:path: "../"
2631

2732
CHECKOUT OPTIONS:
2833
SDWebImage:
2934
:commit: 2bb336bc12a5690dd11988c0bc8bef2c7318508d
30-
:git: [email protected]:rs/SDWebImage.git
35+
:git: "[email protected]:rs/SDWebImage.git"
3136

3237
SPEC CHECKSUMS:
3338
SDWebImage: 8e4aaf5805e954ad426702adf5a8c012a7f581d1
@@ -37,4 +42,4 @@ SPEC CHECKSUMS:
3742

3843
PODFILE CHECKSUM: f8c72633fbc009d6f1bfe12afb37eb2cdaccdb13
3944

40-
COCOAPODS: 1.4.0
45+
COCOAPODS: 1.5.3

Example/SDWebImageYYPlugin.xcodeproj/project.pbxproj

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@
208208
6003F587195388D20070C39A /* Frameworks */,
209209
6003F588195388D20070C39A /* Resources */,
210210
053E29CB027BC9446808DA05 /* [CP] Embed Pods Frameworks */,
211-
1E73D407B46B3CEE69E8FD87 /* [CP] Copy Pods Resources */,
212211
);
213212
buildRules = (
214213
);
@@ -227,8 +226,6 @@
227226
6003F5AA195388D20070C39A /* Sources */,
228227
6003F5AB195388D20070C39A /* Frameworks */,
229228
6003F5AC195388D20070C39A /* Resources */,
230-
005F82905EBA1CA9A655C3C6 /* [CP] Embed Pods Frameworks */,
231-
371F4E9CA414CF22DBE83FA3 /* [CP] Copy Pods Resources */,
232229
);
233230
buildRules = (
234231
);
@@ -297,21 +294,6 @@
297294
/* End PBXResourcesBuildPhase section */
298295

299296
/* Begin PBXShellScriptBuildPhase section */
300-
005F82905EBA1CA9A655C3C6 /* [CP] Embed Pods Frameworks */ = {
301-
isa = PBXShellScriptBuildPhase;
302-
buildActionMask = 2147483647;
303-
files = (
304-
);
305-
inputPaths = (
306-
);
307-
name = "[CP] Embed Pods Frameworks";
308-
outputPaths = (
309-
);
310-
runOnlyForDeploymentPostprocessing = 0;
311-
shellPath = /bin/sh;
312-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-SDWebImageYYPlugin_Tests/Pods-SDWebImageYYPlugin_Tests-frameworks.sh\"\n";
313-
showEnvVarsInLog = 0;
314-
};
315297
053E29CB027BC9446808DA05 /* [CP] Embed Pods Frameworks */ = {
316298
isa = PBXShellScriptBuildPhase;
317299
buildActionMask = 2147483647;
@@ -336,36 +318,6 @@
336318
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-SDWebImageYYPlugin_Example/Pods-SDWebImageYYPlugin_Example-frameworks.sh\"\n";
337319
showEnvVarsInLog = 0;
338320
};
339-
1E73D407B46B3CEE69E8FD87 /* [CP] Copy Pods Resources */ = {
340-
isa = PBXShellScriptBuildPhase;
341-
buildActionMask = 2147483647;
342-
files = (
343-
);
344-
inputPaths = (
345-
);
346-
name = "[CP] Copy Pods Resources";
347-
outputPaths = (
348-
);
349-
runOnlyForDeploymentPostprocessing = 0;
350-
shellPath = /bin/sh;
351-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-SDWebImageYYPlugin_Example/Pods-SDWebImageYYPlugin_Example-resources.sh\"\n";
352-
showEnvVarsInLog = 0;
353-
};
354-
371F4E9CA414CF22DBE83FA3 /* [CP] Copy Pods Resources */ = {
355-
isa = PBXShellScriptBuildPhase;
356-
buildActionMask = 2147483647;
357-
files = (
358-
);
359-
inputPaths = (
360-
);
361-
name = "[CP] Copy Pods Resources";
362-
outputPaths = (
363-
);
364-
runOnlyForDeploymentPostprocessing = 0;
365-
shellPath = /bin/sh;
366-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-SDWebImageYYPlugin_Tests/Pods-SDWebImageYYPlugin_Tests-resources.sh\"\n";
367-
showEnvVarsInLog = 0;
368-
};
369321
56E885A70EC351C1F1512B57 /* [CP] Check Pods Manifest.lock */ = {
370322
isa = PBXShellScriptBuildPhase;
371323
buildActionMask = 2147483647;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* This file is part of the SDWebImage-YYImage package.
3+
*
4+
* For the full copyright and license information, please view the LICENSE
5+
* file that was distributed with this source code.
6+
*/
7+
8+
#import <YYImage/YYImage.h>
9+
#import <SDWebImage/SDImageCoder.h>
10+
11+
/**
12+
YYImageEncoder && YYImageDecoder bridge to supports SDWebImage coder protocol. This class use YYImage's decoding && encoding system. It supports any image format listed in `YYImageType`.
13+
@note This class conforms to `SDProgressiveImageCoder` && `SDAnimatedImageCoder`, supports static progressive decoding and animation decoding. However, it does not supports progressive animation decoding like `SDImageGIFCoder`. Which means you can not use any method in `SDAnimatedImageProvider` protocol if you create the instance with `initIncrementalWithOptions:` method.
14+
*/
15+
@interface SDImageYYCoder : NSObject <SDImageCoder, SDProgressiveImageCoder, SDAnimatedImageCoder>
16+
17+
@property (nonatomic, class, readonly, nonnull) SDImageYYCoder *sharedCoder;
18+
19+
/**
20+
The wrapped `YYImageDecoder` instance, to support static progressive and animation decoding.
21+
@note This property is only useful for static progressive or animation decoding. For normal decoding process, we will use a temporary `YYImageDecoder` to do decoding because it does not need to keep decoded context.
22+
*/
23+
@property (nonatomic, strong, readonly, nonnull) YYImageDecoder *decoder;
24+
25+
@end
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* This file is part of the SDWebImage-YYImage package.
3+
*
4+
* For the full copyright and license information, please view the LICENSE
5+
* file that was distributed with this source code.
6+
*/
7+
8+
#import "SDImageYYCoder.h"
9+
10+
static inline YYImageType YYImageTypeFromSDImageFormat(SDImageFormat format) {
11+
switch (format) {
12+
case SDImageFormatJPEG:
13+
return YYImageTypeJPEG;
14+
case SDImageFormatPNG:
15+
return YYImageTypePNG;
16+
case SDImageFormatGIF:
17+
return YYImageTypeGIF;
18+
case SDImageFormatTIFF:
19+
return YYImageTypeTIFF;
20+
case SDImageFormatWebP:
21+
return YYImageTypeWebP;
22+
default:
23+
return YYImageTypeUnknown;
24+
}
25+
}
26+
27+
@interface SDImageYYCoder ()
28+
29+
@property (nonatomic, strong) YYImageDecoder *decoder;
30+
31+
@end
32+
33+
@implementation SDImageYYCoder
34+
35+
+ (SDImageYYCoder *)sharedCoder {
36+
static dispatch_once_t onceToken;
37+
static SDImageYYCoder *coder;
38+
dispatch_once(&onceToken, ^{
39+
coder = [[SDImageYYCoder alloc] init];
40+
});
41+
42+
return coder;
43+
}
44+
45+
- (instancetype)init {
46+
self = [super init];
47+
if (self) {
48+
self.decoder = [[YYImageDecoder alloc] initWithScale:1];
49+
}
50+
return self;
51+
}
52+
53+
#pragma mark - SDImageCoder
54+
55+
- (BOOL)canDecodeFromData:(NSData *)data {
56+
YYImageType type = YYImageDetectType((__bridge CFDataRef)(data));
57+
return type != YYImageTypeUnknown;
58+
}
59+
60+
- (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)options {
61+
if (!data) {
62+
return nil;
63+
}
64+
CGFloat scale = 1;
65+
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
66+
if (scaleFactor != nil) {
67+
scale = [scaleFactor doubleValue];
68+
if (scale < 1) {
69+
scale = 1;
70+
}
71+
}
72+
73+
YYImageDecoder *decoder = [YYImageDecoder decoderWithData:data scale:scale];
74+
YYImageFrame *frame = [decoder frameAtIndex:0 decodeForDisplay:NO];
75+
76+
return frame.image;
77+
}
78+
79+
- (BOOL)canEncodeToFormat:(SDImageFormat)format {
80+
YYImageType type = YYImageTypeFromSDImageFormat(format);
81+
return type != YYImageTypeUnknown;
82+
}
83+
84+
- (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format options:(SDImageCoderOptions *)options {
85+
double compressionQuality = 1;
86+
if (options[SDImageCoderEncodeCompressionQuality]) {
87+
compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue];
88+
}
89+
90+
YYImageType type = YYImageTypeFromSDImageFormat(format);
91+
NSData *data = [YYImageEncoder encodeImage:image type:type quality:compressionQuality];
92+
93+
return data;
94+
}
95+
96+
#pragma mark - SDProgressiveImageCoder
97+
98+
- (BOOL)canIncrementalDecodeFromData:(nullable NSData *)data {
99+
YYImageType type = YYImageDetectType((__bridge CFDataRef)(data));
100+
return type != YYImageTypeUnknown;
101+
}
102+
103+
- (instancetype)initIncrementalWithOptions:(SDImageCoderOptions *)options {
104+
self = [super init];
105+
if (self) {
106+
CGFloat scale = 1;
107+
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
108+
if (scaleFactor != nil) {
109+
scale = [scaleFactor doubleValue];
110+
if (scale < 1) {
111+
scale = 1;
112+
}
113+
}
114+
self.decoder = [[YYImageDecoder alloc] initWithScale:scale];
115+
}
116+
117+
return self;
118+
}
119+
120+
- (void)updateIncrementalData:(NSData *)data finished:(BOOL)finished {
121+
if (self.decoder.isFinalized) {
122+
return;
123+
}
124+
125+
[self.decoder updateData:data final:finished];
126+
}
127+
128+
- (UIImage *)incrementalDecodedImageWithOptions:(SDImageCoderOptions *)options {
129+
YYImageFrame *frame = [self.decoder frameAtIndex:0 decodeForDisplay:NO];
130+
131+
return frame.image;
132+
}
133+
134+
#pragma mark - SDAnimatedImageCoder
135+
136+
- (instancetype)initWithAnimatedImageData:(NSData *)data options:(SDImageCoderOptions *)options {
137+
if (!data) {
138+
return nil;
139+
}
140+
self = [super init];
141+
if (self) {
142+
CGFloat scale = 1;
143+
NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor];
144+
if (scaleFactor != nil) {
145+
scale = [scaleFactor doubleValue];
146+
if (scale < 1) {
147+
scale = 1;
148+
}
149+
}
150+
YYImageDecoder *decoder = [YYImageDecoder decoderWithData:data scale:scale];
151+
if (!decoder) {
152+
return nil;
153+
}
154+
self.decoder = decoder;
155+
}
156+
return self;
157+
}
158+
159+
- (NSData *)animatedImageData {
160+
return self.decoder.data;
161+
}
162+
163+
- (NSUInteger)animatedImageFrameCount {
164+
return self.decoder.frameCount;
165+
}
166+
167+
- (NSUInteger)animatedImageLoopCount {
168+
return self.decoder.loopCount;
169+
}
170+
171+
- (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index {
172+
YYImageFrame *frame = [self.decoder frameAtIndex:index decodeForDisplay:NO];
173+
return frame.image;
174+
}
175+
176+
- (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index {
177+
return [self.decoder frameDurationAtIndex:index];
178+
}
179+
180+
@end

SDWebImageYYPlugin/Module/SDWebImageYYPlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#import <SDWebImageYYPlugin/SDAnimatedImage+YYAddtions.h>
2323
#import <SDWebImageYYPlugin/YYAnimatedImageView+WebCache.h>
2424
#import <SDWebImageYYPlugin/YYImage+SDAddtions.h>
25+
#import <SDWebImageYYPlugin/SDImageYYCoder.h>
2526
#endif
2627

2728
FOUNDATION_EXPORT double SDWebImageYYPluginVersionNumber;

0 commit comments

Comments
 (0)