CTAssetThumbnailView.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "CTAssetsPickerDefines.h"
  22. #import "CTAssetThumbnailView.h"
  23. #import "CTAssetThumbnailOverlay.h"
  24. #import "PHAsset+CTAssetsPickerController.h"
  25. #import "NSDateFormatter+CTAssetsPickerController.h"
  26. @interface CTAssetThumbnailView ()
  27. @property (nonatomic, strong) CTAssetThumbnailOverlay *overlay;
  28. @property (nonatomic, strong) UIImageView *imageView;
  29. @property (nonatomic, strong) UIImageView *backgroundView;
  30. @property (nonatomic, assign) BOOL didSetupConstraints;
  31. @end
  32. @implementation CTAssetThumbnailView
  33. - (instancetype)initWithFrame:(CGRect)frame
  34. {
  35. if (self = [super initWithFrame:frame])
  36. {
  37. _showsDuration = YES;
  38. self.opaque = YES;
  39. self.clipsToBounds = YES;
  40. self.isAccessibilityElement = NO;
  41. [self setupViews];
  42. }
  43. return self;
  44. }
  45. #pragma markt - Setup
  46. - (void)setupViews
  47. {
  48. self.backgroundColor = CTAssetsPikcerThumbnailBackgroundColor;
  49. UIImageView *backgroundView = [UIImageView new];
  50. backgroundView.contentMode = UIViewContentModeCenter;
  51. backgroundView.tintColor = CTAssetsPikcerThumbnailTintColor;
  52. self.backgroundView = backgroundView;
  53. UIImageView *imageView = [UIImageView new];
  54. imageView.contentMode = UIViewContentModeScaleAspectFill;
  55. self.imageView = imageView;
  56. [self addSubview:self.backgroundView];
  57. [self addSubview:self.imageView];
  58. }
  59. #pragma markt - Setters
  60. - (void)setBackgroundImage:(UIImage *)backgroundImage
  61. {
  62. _backgroundImage = backgroundImage;
  63. self.backgroundView.image = backgroundImage;
  64. }
  65. #pragma markt - Override set bounds
  66. -(void)setBounds:(CGRect)bounds
  67. {
  68. super.bounds = bounds;
  69. self.overlay.frame = bounds;
  70. [self.overlay setNeedsDisplay];
  71. }
  72. #pragma mark - Update auto layout constraints
  73. - (void)updateConstraints
  74. {
  75. if (!self.didSetupConstraints)
  76. {
  77. [self.backgroundView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  78. [self.imageView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  79. self.didSetupConstraints = YES;
  80. }
  81. [super updateConstraints];
  82. }
  83. #pragma - Bind asset and image
  84. - (void)bind:(UIImage *)image asset:(PHAsset *)asset;
  85. {
  86. [self setupOverlayForAsset:asset];
  87. self.imageView.image = image;
  88. self.backgroundView.hidden = (image != nil);
  89. [self setNeedsUpdateConstraints];
  90. [self updateConstraintsIfNeeded];
  91. }
  92. - (void)setupOverlayForAsset:(PHAsset *)asset
  93. {
  94. if (asset.ctassetsPickerIsVideo)
  95. {
  96. if (!self.overlay) {
  97. self.overlay = [[CTAssetThumbnailOverlay alloc] initWithFrame:self.bounds];
  98. [self addSubview:self.overlay];
  99. }
  100. NSString *duration = nil;
  101. if (self.showsDuration)
  102. {
  103. NSDateFormatter *df = [NSDateFormatter new];
  104. duration = [df ctassetsPickerStringFromTimeInterval:asset.duration];
  105. }
  106. [self.overlay bind:asset duration:duration];
  107. }
  108. else
  109. {
  110. [self.overlay removeFromSuperview];
  111. self.overlay = nil;
  112. }
  113. }
  114. #pragma - Bind asset collection and image
  115. - (void)bind:(UIImage *)image assetCollection:(PHAssetCollection *)assetCollection;
  116. {
  117. [self setupOverlayForAssetCollection:assetCollection];
  118. self.imageView.image = image;
  119. self.backgroundView.hidden = (image != nil);
  120. [self setNeedsUpdateConstraints];
  121. [self updateConstraintsIfNeeded];
  122. }
  123. - (void)setupOverlayForAssetCollection:(PHAssetCollection *)assetCollection
  124. {
  125. if (assetCollection.assetCollectionType == PHAssetCollectionTypeSmartAlbum &&
  126. assetCollection.assetCollectionSubtype != PHAssetCollectionSubtypeSmartAlbumAllHidden)
  127. {
  128. if (!self.overlay) {
  129. self.overlay = [[CTAssetThumbnailOverlay alloc] initWithFrame:self.bounds];
  130. [self addSubview:self.overlay];
  131. }
  132. [self.overlay bind:assetCollection];
  133. }
  134. else
  135. {
  136. [self.overlay removeFromSuperview];
  137. self.overlay = nil;
  138. }
  139. }
  140. @end