InterfaceController.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "InterfaceController.h"
  9. #import <SDWebImage/SDWebImage.h>
  10. @interface InterfaceController()
  11. @property (weak) IBOutlet WKInterfaceImage *staticImageInterface;
  12. @property (weak) IBOutlet WKInterfaceImage *simpleAnimatedImageInterface;
  13. @property (weak) IBOutlet WKInterfaceImage *animatedImageInterface;
  14. @property (nonatomic, strong) SDAnimatedImagePlayer *player;
  15. @end
  16. @implementation InterfaceController
  17. - (void)awakeWithContext:(id)context {
  18. [super awakeWithContext:context];
  19. // Configure interface objects here.
  20. }
  21. - (void)willActivate {
  22. // This method is called when watch view controller is about to be visible to user
  23. [super willActivate];
  24. [self addMenuItemWithItemIcon:WKMenuItemIconTrash title:@"Clear Cache" action:@selector(clearCache)];
  25. // Static image
  26. NSString *urlString1 = @"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp";
  27. [self.staticImageInterface sd_setImageWithURL:[NSURL URLWithString:urlString1]];
  28. // Simple animated image playback
  29. NSString *urlString2 = @"http://apng.onevcat.com/assets/elephant.png";
  30. [self.simpleAnimatedImageInterface sd_setImageWithURL:[NSURL URLWithString:urlString2] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  31. // `WKInterfaceImage` unlike `UIImageView`. Even the image is animated image, you should explicitly call `startAnimating` to play animation.
  32. [self.simpleAnimatedImageInterface startAnimating];
  33. }];
  34. // Complicated but the best performance animated image playback
  35. // If you use the above method to display this GIF (389 frames), Apple Watch will consume 800+MB and cause OOM
  36. // This is actualy the same backend like `SDAnimatedImageView` on iOS, recommend to use
  37. NSString *urlString3 = @"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif";
  38. [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) {
  39. if (![image isKindOfClass:[SDAnimatedImage class]]) {
  40. return;
  41. }
  42. __weak typeof(self) wself = self;
  43. self.player = [SDAnimatedImagePlayer playerWithProvider:(SDAnimatedImage *)image];
  44. self.player.animationFrameHandler = ^(NSUInteger index, UIImage * _Nonnull frame) {
  45. [wself.animatedImageInterface setImage:frame];
  46. };
  47. [self.player startPlaying];
  48. }];
  49. }
  50. - (void)clearCache {
  51. [SDImageCache.sharedImageCache clearMemory];
  52. [SDImageCache.sharedImageCache clearDiskOnCompletion:nil];
  53. }
  54. - (void)didDeactivate {
  55. // This method is called when watch view controller is no longer visible
  56. [super didDeactivate];
  57. }
  58. @end