From 9461bd5c119a0f96828ca36f9c22b3eefeaad0f5 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 4 Nov 2019 03:53:18 +0800 Subject: [PATCH] Update the WatchKit demo with the `SDAnimatedPlayer` to show the simple usage --- .../InterfaceController.m | 40 ++++++++++++++++--- .../Base.lproj/Interface.storyboard | 18 +++++---- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/Examples/SDWebImage Watch Demo Extension/InterfaceController.m b/Examples/SDWebImage Watch Demo Extension/InterfaceController.m index ce673432..6bfd08d2 100644 --- a/Examples/SDWebImage Watch Demo Extension/InterfaceController.m +++ b/Examples/SDWebImage Watch Demo Extension/InterfaceController.m @@ -13,7 +13,10 @@ @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 @@ -31,12 +34,39 @@ // This method is called when watch view controller is about to be visible to user [super willActivate]; - NSString *urlString = @"http://apng.onevcat.com/assets/elephant.png"; - WKInterfaceImage *imageInterface = self.imageInterface; - [imageInterface sd_setImageWithURL:[NSURL URLWithString:urlString] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { + [self addMenuItemWithItemIcon:WKMenuItemIconTrash title:@"Clear Cache" action:@selector(clearCache)]; + + // 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. - [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 { diff --git a/Examples/SDWebImage Watch Demo/Base.lproj/Interface.storyboard b/Examples/SDWebImage Watch Demo/Base.lproj/Interface.storyboard index 133cad27..313802b5 100644 --- a/Examples/SDWebImage Watch Demo/Base.lproj/Interface.storyboard +++ b/Examples/SDWebImage Watch Demo/Base.lproj/Interface.storyboard @@ -1,12 +1,10 @@ - - - - + + - - + + @@ -14,10 +12,14 @@ - + + + - + + +