2016-06-13 14:14:02 +08:00
|
|
|
/*
|
|
|
|
* This file is part of the SDWebImage package.
|
|
|
|
* (c) Olivier Poitrey <rs@dailymotion.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
2016-06-13 05:53:08 +08:00
|
|
|
|
|
|
|
#import "InterfaceController.h"
|
2018-04-19 16:23:17 +08:00
|
|
|
#import <SDWebImage/SDWebImage.h>
|
2016-06-13 05:53:08 +08:00
|
|
|
|
|
|
|
@interface InterfaceController()
|
|
|
|
|
2019-11-04 03:53:18 +08:00
|
|
|
@property (weak) IBOutlet WKInterfaceImage *staticImageInterface;
|
|
|
|
@property (weak) IBOutlet WKInterfaceImage *simpleAnimatedImageInterface;
|
|
|
|
@property (weak) IBOutlet WKInterfaceImage *animatedImageInterface;
|
|
|
|
@property (nonatomic, strong) SDAnimatedImagePlayer *player;
|
2016-06-13 05:53:08 +08:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation InterfaceController
|
|
|
|
|
|
|
|
- (void)awakeWithContext:(id)context {
|
|
|
|
[super awakeWithContext:context];
|
|
|
|
|
|
|
|
// Configure interface objects here.
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)willActivate {
|
|
|
|
// This method is called when watch view controller is about to be visible to user
|
|
|
|
[super willActivate];
|
|
|
|
|
2019-11-04 03:53:18 +08:00
|
|
|
[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) {
|
2018-05-24 01:48:08 +08:00
|
|
|
// `WKInterfaceImage` unlike `UIImageView`. Even the image is animated image, you should explicitly call `startAnimating` to play animation.
|
2019-11-04 03:53:18 +08:00
|
|
|
[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];
|
2016-06-13 05:53:08 +08:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2019-11-04 03:53:18 +08:00
|
|
|
- (void)clearCache {
|
|
|
|
[SDImageCache.sharedImageCache clearMemory];
|
|
|
|
[SDImageCache.sharedImageCache clearDiskOnCompletion:nil];
|
|
|
|
}
|
|
|
|
|
2016-06-13 05:53:08 +08:00
|
|
|
- (void)didDeactivate {
|
|
|
|
// This method is called when watch view controller is no longer visible
|
|
|
|
[super didDeactivate];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|