Update the WatchKit demo with the `SDAnimatedPlayer` to show the simple usage

This commit is contained in:
DreamPiggy 2019-11-04 03:53:18 +08:00
parent b0d6825842
commit 9461bd5c11
2 changed files with 45 additions and 13 deletions

View File

@ -13,7 +13,10 @@
@interface InterfaceController() @interface InterfaceController()
@property (weak) IBOutlet WKInterfaceImage *imageInterface; @property (weak) IBOutlet WKInterfaceImage *staticImageInterface;
@property (weak) IBOutlet WKInterfaceImage *simpleAnimatedImageInterface;
@property (weak) IBOutlet WKInterfaceImage *animatedImageInterface;
@property (nonatomic, strong) SDAnimatedImagePlayer *player;
@end @end
@ -31,12 +34,39 @@
// This method is called when watch view controller is about to be visible to user // This method is called when watch view controller is about to be visible to user
[super willActivate]; [super willActivate];
NSString *urlString = @"http://apng.onevcat.com/assets/elephant.png"; [self addMenuItemWithItemIcon:WKMenuItemIconTrash title:@"Clear Cache" action:@selector(clearCache)];
WKInterfaceImage *imageInterface = self.imageInterface;
[imageInterface sd_setImageWithURL:[NSURL URLWithString:urlString] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { // Static image
NSString *urlString1 = @"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp";
[self.staticImageInterface sd_setImageWithURL:[NSURL URLWithString:urlString1]];
// Simple animated image playback
NSString *urlString2 = @"http://apng.onevcat.com/assets/elephant.png";
[self.simpleAnimatedImageInterface sd_setImageWithURL:[NSURL URLWithString:urlString2] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
// `WKInterfaceImage` unlike `UIImageView`. Even the image is animated image, you should explicitly call `startAnimating` to play animation. // `WKInterfaceImage` unlike `UIImageView`. Even the image is animated image, you should explicitly call `startAnimating` to play animation.
[imageInterface startAnimating]; [self.simpleAnimatedImageInterface startAnimating];
}]; }];
// Complicated but the best performance animated image playback
// If you use the above method to display this GIF (389 frames), Apple Watch will consume 800+MB and cause OOM
// This is actualy the same backend like `SDAnimatedImageView` on iOS, recommend to use
NSString *urlString3 = @"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif";
[self.animatedImageInterface sd_setImageWithURL:[NSURL URLWithString:urlString3] placeholderImage:nil options:SDWebImageProgressiveLoad context:@{SDWebImageContextAnimatedImageClass : SDAnimatedImage.class} progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
if (![image isKindOfClass:[SDAnimatedImage class]]) {
return;
}
__weak typeof(self) wself = self;
self.player = [SDAnimatedImagePlayer playerWithProvider:(SDAnimatedImage *)image];
self.player.animationFrameHandler = ^(NSUInteger index, UIImage * _Nonnull frame) {
[wself.animatedImageInterface setImage:frame];
};
[self.player startPlaying];
}];
}
- (void)clearCache {
[SDImageCache.sharedImageCache clearMemory];
[SDImageCache.sharedImageCache clearDiskOnCompletion:nil];
} }
- (void)didDeactivate { - (void)didDeactivate {

View File

@ -1,12 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="14490.70" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="AgC-eL-Hgc"> <document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="15505" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="AgC-eL-Hgc">
<device id="watch38" orientation="portrait"> <device id="watch38"/>
<adaptation id="fullscreen"/>
</device>
<dependencies> <dependencies>
<deployment identifier="watchOS"/> <deployment identifier="watchOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15509"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="14490.21"/> <plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="15501"/>
</dependencies> </dependencies>
<scenes> <scenes>
<!--Interface Controller--> <!--Interface Controller-->
@ -14,10 +12,14 @@
<objects> <objects>
<controller id="AgC-eL-Hgc" customClass="InterfaceController"> <controller id="AgC-eL-Hgc" customClass="InterfaceController">
<items> <items>
<imageView height="150" alignment="left" id="tBV-Mv-OtJ"/> <imageView alignment="left" contentMode="scaleAspectFit" id="Tox-Rh-1mN"/>
<imageView alignment="left" contentMode="scaleAspectFit" id="tBV-Mv-OtJ"/>
<imageView alignment="left" contentMode="scaleAspectFit" id="7dg-wI-usi"/>
</items> </items>
<connections> <connections>
<outlet property="imageInterface" destination="tBV-Mv-OtJ" id="uId-u2-it3"/> <outlet property="animatedImageInterface" destination="7dg-wI-usi" id="eDq-ra-x9k"/>
<outlet property="simpleAnimatedImageInterface" destination="tBV-Mv-OtJ" id="Wzc-db-ghd"/>
<outlet property="staticImageInterface" destination="Tox-Rh-1mN" id="M4p-h5-1ld"/>
</connections> </connections>
</controller> </controller>
</objects> </objects>