InterfaceController.m 3.3 KB

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