Skip to content

Commit 0a95ff5

Browse files
committed
Update the hack for watchOS AnimatedImage to compatible for watchOS 6.1 changes
1 parent 847673d commit 0a95ff5

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

SDWebImageSwiftUI/Classes/ObjC/SDAnimatedImageInterface.m

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,23 @@
1111

1212
#pragma mark - SPI
1313

14-
#define kCGImageAnimationStatus_Uninitialized -1
14+
static UIImage * SharedEmptyImage(void) {
15+
// This is used for placeholder on `WKInterfaceImage`
16+
// Do not using `[UIImage new]` because WatchKit will ignore it
17+
static dispatch_once_t onceToken;
18+
static UIImage *image;
19+
dispatch_once(&onceToken, ^{
20+
UIColor *color = UIColor.clearColor;
21+
CGRect rect = WKInterfaceDevice.currentDevice.screenBounds;
22+
UIGraphicsBeginImageContext(rect.size);
23+
CGContextRef context = UIGraphicsGetCurrentContext();
24+
CGContextSetFillColorWithColor(context, [color CGColor]);
25+
CGContextFillRect(context, rect);
26+
image = UIGraphicsGetImageFromCurrentImageContext();
27+
UIGraphicsEndImageContext();
28+
});
29+
return image;
30+
}
1531

1632
@protocol CALayerProtocol <NSObject>
1733
@property (nullable, strong) id contents;
@@ -30,8 +46,9 @@ @protocol UIViewProtocol <NSObject>
3046

3147
@end
3248

33-
@protocol UIImageViewProtocol <NSObject>
49+
@protocol UIImageViewProtocol <UIViewProtocol>
3450

51+
@property (nullable, nonatomic, strong) UIImage *image;
3552
- (void)startAnimating;
3653
- (void)stopAnimating;
3754
@property (nonatomic, readonly, getter=isAnimating) BOOL animating;
@@ -43,7 +60,7 @@ @interface WKInterfaceObject ()
4360
// This is needed for dynamic created WKInterfaceObject, like `WKInterfaceMap`
4461
- (instancetype)_initForDynamicCreationWithInterfaceProperty:(NSString *)property;
4562
// This is remote UIView
46-
@property (nonatomic, strong, readonly) id<UIViewProtocol> _interfaceView;
63+
@property (nonatomic, strong, readonly) id<UIImageViewProtocol> _interfaceView;
4764

4865
@end
4966

@@ -80,28 +97,10 @@ - (NSDictionary *)interfaceDescriptionForDynamicCreation {
8097
return @{
8198
@"type" : @"image",
8299
@"property" : self.interfaceProperty,
83-
@"image" : [self.class sharedEmptyImage]
100+
@"image" : SharedEmptyImage()
84101
};
85102
}
86103

87-
+ (UIImage *)sharedEmptyImage {
88-
// This is used for placeholder on `WKInterfaceImage`
89-
// Do not using `[UIImage new]` because WatchKit will ignore it
90-
static dispatch_once_t onceToken;
91-
static UIImage *image;
92-
dispatch_once(&onceToken, ^{
93-
UIColor *color = UIColor.clearColor;
94-
CGRect rect = CGRectMake(0, 0, 1, 1);
95-
UIGraphicsBeginImageContext(rect.size);
96-
CGContextRef context = UIGraphicsGetCurrentContext();
97-
CGContextSetFillColorWithColor(context, [color CGColor]);
98-
CGContextFillRect(context, rect);
99-
image = UIGraphicsGetImageFromCurrentImageContext();
100-
UIGraphicsEndImageContext();
101-
});
102-
return image;
103-
}
104-
105104
- (void)setImage:(UIImage *)image {
106105
if (_image == image) {
107106
return;
@@ -115,6 +114,7 @@ - (void)setImage:(UIImage *)image {
115114
self.currentLoopCount = 0;
116115

117116
[super setImage:image];
117+
[self _interfaceView].image = image;
118118
if ([image.class conformsToProtocol:@protocol(SDAnimatedImage)]) {
119119
// Create animted player
120120
self.player = [SDAnimatedImagePlayer playerWithProvider:(id<SDAnimatedImage>)image];
@@ -148,30 +148,19 @@ - (void)setImage:(UIImage *)image {
148148
sself.currentLoopCount = loopCount;
149149
};
150150

151-
// Update should animate
152-
[self updateShouldAnimate];
153-
if (self.shouldAnimate) {
154-
[self startAnimating];
155-
}
151+
// Start animating
152+
[self startAnimating];
156153

157154
[self displayLayer:self.imageViewLayer];
158155
}
159156
}
160157

161158
- (void)updateAnimation {
162159
[self updateShouldAnimate];
163-
if (!self.player) {
164-
return;
165-
}
166-
// Filter automatically animating changes
167160
if (self.shouldAnimate && self.isAnimating) {
168-
[self.player startPlaying];
169-
} else if (!self.shouldAnimate && !self.isAnimating) {
170-
if (self.resetFrameIndexWhenStopped) {
171-
[self.player stopPlaying];
172-
} else {
173-
[self.player pausePlaying];
174-
}
161+
[self startAnimating];
162+
} else {
163+
[self stopAnimating];
175164
}
176165
}
177166

@@ -198,7 +187,10 @@ - (void)updateShouldAnimate
198187
- (void)startAnimating {
199188
self.animating = YES;
200189
if (self.player) {
201-
[self.player startPlaying];
190+
[self updateShouldAnimate];
191+
if (self.shouldAnimate) {
192+
[self.player startPlaying];
193+
}
202194
} else if (_image.images.count > 0) {
203195
[super startAnimating];
204196
}

0 commit comments

Comments
 (0)