ViewController.m 3.2 KB

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