ViewController.m 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "ViewController.h"
  9. #import <SDWebImage/SDWebImage.h>
  10. @interface ViewController ()
  11. @property (weak) IBOutlet NSImageView *imageView1;
  12. @property (weak) IBOutlet NSImageView *imageView2;
  13. @property (weak) IBOutlet SDAnimatedImageView *imageView3;
  14. @property (weak) IBOutlet SDAnimatedImageView *imageView4;
  15. @property (weak) IBOutlet NSButton *clearCacheButton;
  16. @end
  17. @implementation ViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // For animated GIF rendering, set `animates` to YES or will only show the first frame
  21. self.imageView2.animates = YES; // `SDAnimatedImageRep` can be used for built-in `NSImageView` to support better GIF & APNG rendering as well. No need `SDAnimatedImageView`
  22. self.imageView4.animates = YES;
  23. // NSImageView + Static Image
  24. self.imageView1.sd_imageIndicator = SDWebImageProgressIndicator.defaultIndicator;
  25. [self.imageView1 sd_setImageWithURL:[NSURL URLWithString:@"https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Landscape_2.jpg"] placeholderImage:nil options:SDWebImageProgressiveLoad];
  26. // NSImageView + Animated Image
  27. self.imageView2.sd_imageIndicator = SDWebImageActivityIndicator.largeIndicator;
  28. [self.imageView2 sd_setImageWithURL:[NSURL URLWithString:@"https:raw.githubusercontent.com/onevcat/APNGKit/master/TestImages/APNG-cube.apng"]];
  29. NSMenu *menu1 = [[NSMenu alloc] initWithTitle:@"Toggle Animation"];
  30. NSMenuItem *item1 = [menu1 addItemWithTitle:@"Toggle Animation" action:@selector(toggleAnimation:) keyEquivalent:@""];
  31. item1.tag = 1;
  32. self.imageView2.menu = menu1;
  33. // SDAnimatedImageView + Static Image
  34. [self.imageView3 sd_setImageWithURL:[NSURL URLWithString:@"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png"]];
  35. // SDAnimatedImageView + Animated Image
  36. self.imageView4.sd_imageTransition = SDWebImageTransition.fadeTransition;
  37. self.imageView4.imageScaling = NSImageScaleProportionallyUpOrDown;
  38. self.imageView4.imageAlignment = NSImageAlignLeft; // supports NSImageView's layout properties
  39. [self.imageView4 sd_setImageWithURL:[NSURL URLWithString:@"http://littlesvr.ca/apng/images/SteamEngine.webp"]];
  40. NSMenu *menu2 = [[NSMenu alloc] initWithTitle:@"Toggle Animation"];
  41. NSMenuItem *item2 = [menu2 addItemWithTitle:@"Toggle Animation" action:@selector(toggleAnimation:) keyEquivalent:@""];
  42. item2.tag = 2;
  43. self.imageView4.menu = menu2;
  44. self.clearCacheButton.target = self;
  45. self.clearCacheButton.action = @selector(clearCacheButtonClicked:);
  46. [self.clearCacheButton sd_setImageWithURL:[NSURL URLWithString:@"https://png.icons8.com/color/100/000000/delete-sign.png"]];
  47. [self.clearCacheButton sd_setAlternateImageWithURL:[NSURL URLWithString:@"https://png.icons8.com/color/100/000000/checkmark.png"]];
  48. }
  49. - (void)clearCacheButtonClicked:(NSResponder *)sender {
  50. NSButton *button = (NSButton *)sender;
  51. button.state = NSControlStateValueOn;
  52. [[SDImageCache sharedImageCache] clearMemory];
  53. [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
  54. button.state = NSControlStateValueOff;
  55. }];
  56. }
  57. - (void)toggleAnimation:(NSMenuItem *)sender {
  58. NSImageView *imageView = sender.tag == 1 ? self.imageView2 : self.imageView4;
  59. if (imageView.animates) {
  60. imageView.animates = NO;
  61. } else {
  62. imageView.animates = YES;
  63. }
  64. }
  65. - (void)setRepresentedObject:(id)representedObject {
  66. [super setRepresentedObject:representedObject];
  67. // Update the view, if already loaded.
  68. }
  69. @end