DetailViewController.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. BOOL isHDR = [self.imageURL.absoluteString containsString:@"HDR"];
  20. if (@available(iOS 17.0, *)) {
  21. self.imageView.preferredImageDynamicRange = isHDR ? UIImageDynamicRangeHigh : UIImageDynamicRangeUnspecified;
  22. }
  23. SDWebImageContext *context = @{
  24. SDWebImageContextImageDecodeToHDR: @(isHDR)
  25. };
  26. [self.imageView sd_setImageWithURL:self.imageURL
  27. placeholderImage:nil
  28. options:SDWebImageFromLoaderOnly | SDWebImageScaleDownLargeImages
  29. context:context
  30. progress:nil
  31. completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  32. NSLog(@"isHighDynamicRange %@", @(image.sd_isHighDynamicRange));
  33. }];
  34. self.imageView.shouldCustomLoopCount = YES;
  35. self.imageView.animationRepeatCount = 0;
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. [self configureView];
  40. self.navigationItem.rightBarButtonItem = [UIBarButtonItem.alloc initWithTitle:@"Toggle Animation"
  41. style:UIBarButtonItemStylePlain
  42. target:self
  43. action:@selector(toggleAnimation:)];
  44. // Add a secret title click action to apply tint color
  45. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  46. [button addTarget:self
  47. action:@selector(toggleTint:)
  48. forControlEvents:UIControlEventTouchUpInside];
  49. [button setTitle:@"Tint" forState:UIControlStateNormal];
  50. self.navigationItem.titleView = button;
  51. }
  52. - (void)toggleTint:(UIResponder *)sender {
  53. // tint for non-opaque animation
  54. if (!self.imageView.isAnimating) {
  55. return;
  56. }
  57. SDAnimatedImage *animatedImage = (SDAnimatedImage *)self.imageView.image;
  58. if (animatedImage.sd_imageFormat == SDImageFormatGIF) {
  59. // GIF is opaque
  60. return;
  61. }
  62. BOOL containsAlpha = [SDImageCoderHelper CGImageContainsAlpha:animatedImage.CGImage];
  63. if (!containsAlpha) {
  64. return;
  65. }
  66. if (self.tintApplied) {
  67. self.imageView.animationTransformer = nil;
  68. } else {
  69. self.imageView.animationTransformer = [SDImageTintTransformer transformerWithColor:UIColor.blackColor];
  70. }
  71. self.tintApplied = !self.tintApplied;
  72. // refresh
  73. UIImage *image = self.imageView.image;
  74. self.imageView.image = nil;
  75. self.imageView.image = image;
  76. }
  77. - (void)toggleAnimation:(UIResponder *)sender {
  78. self.imageView.isAnimating ? [self.imageView stopAnimating] : [self.imageView startAnimating];
  79. }
  80. @end