DetailViewController.m 2.8 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 "DetailViewController.h"
  9. #import <SDWebImage/SDWebImage.h>
  10. @interface DetailViewController ()
  11. @property (strong, nonatomic) IBOutlet SDAnimatedImageView *imageView;
  12. @property (assign) BOOL tintApplied;
  13. @end
  14. @implementation DetailViewController
  15. - (void)configureView {
  16. if (!self.imageView.sd_imageIndicator) {
  17. self.imageView.sd_imageIndicator = SDWebImageProgressIndicator.defaultIndicator;
  18. }
  19. [self.imageView sd_setImageWithURL:self.imageURL
  20. placeholderImage:nil
  21. options:SDWebImageProgressiveLoad | SDWebImageScaleDownLargeImages
  22. context:@{SDWebImageContextImageForceDecodePolicy: @(SDImageForceDecodePolicyNever)}
  23. ];
  24. self.imageView.shouldCustomLoopCount = YES;
  25. self.imageView.animationRepeatCount = 0;
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. [self configureView];
  30. self.navigationItem.rightBarButtonItem = [UIBarButtonItem.alloc initWithTitle:@"Toggle Animation"
  31. style:UIBarButtonItemStylePlain
  32. target:self
  33. action:@selector(toggleAnimation:)];
  34. // Add a secret title click action to apply tint color
  35. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  36. [button addTarget:self
  37. action:@selector(toggleTint:)
  38. forControlEvents:UIControlEventTouchUpInside];
  39. [button setTitle:@"Tint" forState:UIControlStateNormal];
  40. self.navigationItem.titleView = button;
  41. }
  42. - (void)toggleTint:(UIResponder *)sender {
  43. // tint for non-opaque animation
  44. if (!self.imageView.isAnimating) {
  45. return;
  46. }
  47. SDAnimatedImage *animatedImage = (SDAnimatedImage *)self.imageView.image;
  48. if (animatedImage.sd_imageFormat == SDImageFormatGIF) {
  49. // GIF is opaque
  50. return;
  51. }
  52. BOOL containsAlpha = [SDImageCoderHelper CGImageContainsAlpha:animatedImage.CGImage];
  53. if (!containsAlpha) {
  54. return;
  55. }
  56. if (self.tintApplied) {
  57. self.imageView.animationTransformer = nil;
  58. } else {
  59. self.imageView.animationTransformer = [SDImageTintTransformer transformerWithColor:UIColor.blackColor];
  60. }
  61. self.tintApplied = !self.tintApplied;
  62. // refresh
  63. UIImage *image = self.imageView.image;
  64. self.imageView.image = nil;
  65. self.imageView.image = image;
  66. }
  67. - (void)toggleAnimation:(UIResponder *)sender {
  68. self.imageView.isAnimating ? [self.imageView stopAnimating] : [self.imageView startAnimating];
  69. }
  70. @end