Skip to content

Commit dd8b6d0

Browse files
committed
Fix that YYImage plugin isAllFramesLoaded not work by workaround, update tests
1 parent c7d9bff commit dd8b6d0

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

Example/Tests/SDYYCacheTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ - (void)testCustomImageCache {
6565
- (void)testYYCacheQueryOp {
6666
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache query op works"];
6767
NSData *imageData = [NSData dataWithContentsOfFile:[self testJPEGPath]];
68-
[SDYYCacheTests.sharedCache.diskCache setObject:imageData forKey:kTestImageKeyJPEG];
68+
[SDYYCacheTests.sharedCache.diskCache setData:imageData forKey:kTestImageKeyJPEG];
6969
[SDYYCacheTests.sharedCache queryImageForKey:kTestImageKeyJPEG options:0 context:nil completion:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
7070
expect(image).notTo.beNil();
7171
[expectation fulfill];

Example/Tests/SDYYImageTests.m

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,31 @@ - (void)testYYAnimatedImageViewSetImageWithURL {
3535
expect(error).to.beNil();
3636
expect(originalImageURL).to.equal(imageURL);
3737

38-
expect(imageView.image.class).to.equal(YYImage.class);
38+
expect(image.class).to.equal(YYImage.class);
39+
[expectation fulfill];
40+
}];
41+
[self waitForExpectationsWithCommonTimeout];
42+
}
43+
44+
- (void)testYYAnimatedImageViewSetImageWithURLPreloadAllFrames {
45+
XCTestExpectation *expectation = [self expectationWithDescription:@"YYAnimatedImageView setImageWithURL preloadAllFrames"];
46+
47+
YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] init];
48+
NSURL *originalImageURL = [NSURL URLWithString:kTestAPNGPURL];
49+
50+
[imageView sd_setImageWithURL:originalImageURL
51+
placeholderImage:nil
52+
options:SDWebImagePreloadAllFrames
53+
completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
54+
expect(image).toNot.beNil();
55+
expect(error).to.beNil();
56+
expect(originalImageURL).to.equal(imageURL);
57+
58+
YYImage *animatedImage = (YYImage *)image;
59+
expect(animatedImage.class).to.equal(YYImage.class);
60+
expect(animatedImage.isAllFramesLoaded).beTruthy();
61+
[animatedImage unloadAllFrames];
62+
expect(animatedImage.isAllFramesLoaded).beFalsy();
3963
[expectation fulfill];
4064
}];
4165
[self waitForExpectationsWithCommonTimeout];
@@ -95,7 +119,7 @@ - (void)testSDImageYYCoderProgressiveJPEGWorks {
95119
[SDImageCodersManager.sharedManager addCoder:SDImageYYCoder.sharedCoder];
96120

97121
NSURL *imageURL = [NSURL URLWithString:kTestProgressiveJPEGURL];
98-
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:SDWebImageDownloaderProgressiveLoad progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
122+
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:SDWebImageDownloaderProgressiveLoad context:@{SDWebImageContextAnimatedImageClass : YYImage.class} progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
99123
if (image && data && !error && finished) {
100124
[expectation fulfill];
101125
} else if (finished) {

SDWebImageYYPlugin/Classes/YYImage/YYImageBridge/YYImage+SDAdditions.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#import "YYImage+SDAdditions.h"
9+
#import <objc/runtime.h>
910

1011
@implementation YYImage (SDAdditions)
1112

@@ -22,10 +23,16 @@ - (instancetype)initWithAnimatedCoder:(id<SDAnimatedImageCoder>)animatedCoder sc
2223

2324
- (void)preloadAllFrames {
2425
[self setPreloadAllAnimatedImageFrames:YES];
26+
// YYImage contains bug that don't update ivar value, simple workaround
27+
Ivar ivar = class_getInstanceVariable(self.class, "_preloadAllAnimatedImageFrames");
28+
((void (*)(id, Ivar, BOOL))object_setIvar)(self, ivar, YES);
2529
}
2630

2731
- (void)unloadAllFrames {
2832
[self setPreloadAllAnimatedImageFrames:NO];
33+
// YYImage contains bug that don't update ivar value, simple workaround
34+
Ivar ivar = class_getInstanceVariable(self.class, "_preloadAllAnimatedImageFrames");
35+
((void (*)(id, Ivar, BOOL))object_setIvar)(self, ivar, NO);
2936
}
3037

3138
- (BOOL)isAllFramesLoaded {

0 commit comments

Comments
 (0)