CTAssetThumbnailOverlay.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. MIT License (MIT)
  3. Copyright (c) 2015 Clement CN Tsang
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #import <PureLayout/PureLayout.h>
  21. #import "CTAssetThumbnailOverlay.h"
  22. #import "UIImage+CTAssetsPickerController.h"
  23. #import "PHAsset+CTAssetsPickerController.h"
  24. #import "PHAssetCollection+CTAssetsPickerController.h"
  25. @interface CTAssetThumbnailOverlay ()
  26. @property (nonatomic, strong) UIImageView *gradient;
  27. @property (nonatomic, strong) UIImageView *badge;
  28. @property (nonatomic, strong) UILabel *duration;
  29. @property (nonatomic, assign) BOOL didSetupConstraints;
  30. @end
  31. @implementation CTAssetThumbnailOverlay
  32. - (instancetype)initWithFrame:(CGRect)frame
  33. {
  34. if (self = [super initWithFrame:frame])
  35. {
  36. self.opaque = NO;
  37. self.clipsToBounds = YES;
  38. self.isAccessibilityElement = NO;
  39. [self setupViews];
  40. }
  41. return self;
  42. }
  43. #pragma markt - Setup
  44. - (void)setupViews
  45. {
  46. UIImageView *gradient = [UIImageView newAutoLayoutView];
  47. gradient.image = [UIImage ctassetsPickerImageNamed:@"GridGradient"];
  48. self.gradient = gradient;
  49. [self addSubview:self.gradient];
  50. UIImageView *badge = [UIImageView newAutoLayoutView];
  51. badge.tintColor = [UIColor whiteColor];
  52. self.badge = badge;
  53. [self addSubview:self.badge];
  54. UILabel *duration = [UILabel newAutoLayoutView];
  55. duration.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
  56. duration.textColor = [UIColor whiteColor];
  57. duration.lineBreakMode = NSLineBreakByTruncatingTail;
  58. duration.layoutMargins = UIEdgeInsetsMake(2.5, 2.5, 2.5, 2.5);
  59. self.duration = duration;
  60. [self addSubview:self.duration];
  61. }
  62. #pragma mark - Update auto layout constraints
  63. - (void)updateConstraints
  64. {
  65. if (!self.didSetupConstraints)
  66. {
  67. [self.gradient autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
  68. [self.gradient autoSetDimension:ALDimensionHeight toSize:self.gradient.image.size.height];
  69. [self.badge autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:self.badge.layoutMargins.left];
  70. [self.badge autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:self.badge.layoutMargins.bottom];
  71. [self.duration autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:self.duration.layoutMargins.right];
  72. [self.duration autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:self.duration.layoutMargins.bottom];
  73. self.didSetupConstraints = YES;
  74. }
  75. [super updateConstraints];
  76. }
  77. #pragma - Bind asset and duration
  78. - (void)bind:(PHAsset *)asset duration:(NSString *)duration;
  79. {
  80. self.badge.image = [asset badgeImage];
  81. self.badge.layoutMargins = [self layoutMarginsForAsset:asset];
  82. self.duration.text = duration;
  83. [self setNeedsUpdateConstraints];
  84. [self updateConstraintsIfNeeded];
  85. }
  86. - (UIEdgeInsets)layoutMarginsForAsset:(PHAsset *)asset
  87. {
  88. if (asset.ctassetsPickerIsHighFrameRateVideo)
  89. return UIEdgeInsetsMake(2.5, 2.5, 2.5, 2.5);
  90. else if (asset.ctassetsPickerIsTimelapseVideo)
  91. return UIEdgeInsetsMake(2.5, 2.5, 2.5, 2.5);
  92. else if (asset.ctassetsPickerIsVideo)
  93. return UIEdgeInsetsMake(4.5, 4.5, 4.5, 4.5);
  94. else
  95. return UIEdgeInsetsZero;
  96. }
  97. #pragma - Bind asset collection
  98. - (void)bind:(PHAssetCollection *)assetCollection;
  99. {
  100. self.badge.image = [assetCollection badgeImage];
  101. self.badge.layoutMargins = [self layoutMarginsForAssetCollection:assetCollection];
  102. self.duration.text = nil;
  103. [self setNeedsUpdateConstraints];
  104. [self updateConstraintsIfNeeded];
  105. }
  106. - (UIEdgeInsets)layoutMarginsForAssetCollection:(PHAssetCollection *)assetCollection
  107. {
  108. return UIEdgeInsetsMake(4.0, 4.0, 4.0, 4.0);
  109. }
  110. @end