TZAssetCell.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //
  2. // TZAssetCell.m
  3. // TZImagePickerController
  4. //
  5. // Created by 谭真 on 15/12/24.
  6. // Copyright © 2015年 谭真. All rights reserved.
  7. //
  8. #import "TZAssetCell.h"
  9. #import "TZAssetModel.h"
  10. #import "UIView+Layout.h"
  11. #import "TZImageManager.h"
  12. #import "TZImagePickerController.h"
  13. #import "TZProgressView.h"
  14. @interface TZAssetCell ()
  15. @property (weak, nonatomic) UIImageView *imageView; // The photo / 照片
  16. @property (weak, nonatomic) UIImageView *selectImageView;
  17. @property (weak, nonatomic) UIView *bottomView;
  18. @property (weak, nonatomic) UILabel *timeLength;
  19. @property (nonatomic, weak) UIImageView *videoImgView;
  20. @property (nonatomic, strong) TZProgressView *progressView;
  21. @property (nonatomic, assign) PHImageRequestID bigImageRequestID;
  22. @end
  23. @implementation TZAssetCell
  24. - (void)setModel:(TZAssetModel *)model {
  25. _model = model;
  26. if (iOS8Later) {
  27. self.representedAssetIdentifier = [[TZImageManager manager] getAssetIdentifier:model.asset];
  28. }
  29. PHImageRequestID imageRequestID = [[TZImageManager manager] getPhotoWithAsset:model.asset photoWidth:self.tz_width completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
  30. if (_progressView) {
  31. self.progressView.hidden = YES;
  32. self.imageView.alpha = 1.0;
  33. }
  34. // Set the cell's thumbnail image if it's still showing the same asset.
  35. if (!iOS8Later) {
  36. self.imageView.image = photo; return;
  37. }
  38. if ([self.representedAssetIdentifier isEqualToString:[[TZImageManager manager] getAssetIdentifier:model.asset]]) {
  39. self.imageView.image = photo;
  40. } else {
  41. // NSLog(@"this cell is showing other asset");
  42. [[PHImageManager defaultManager] cancelImageRequest:self.imageRequestID];
  43. }
  44. if (!isDegraded) {
  45. self.imageRequestID = 0;
  46. }
  47. } progressHandler:nil networkAccessAllowed:NO];
  48. if (imageRequestID && self.imageRequestID && imageRequestID != self.imageRequestID) {
  49. [[PHImageManager defaultManager] cancelImageRequest:self.imageRequestID];
  50. // NSLog(@"cancelImageRequest %d",self.imageRequestID);
  51. }
  52. self.imageRequestID = imageRequestID;
  53. self.selectPhotoButton.selected = model.isSelected;
  54. self.selectImageView.image = self.selectPhotoButton.isSelected ? [UIImage imageNamedFromMyBundle:self.photoSelImageName] : [UIImage imageNamedFromMyBundle:self.photoDefImageName];
  55. self.type = (NSInteger)model.type;
  56. // 让宽度/高度小于 最小可选照片尺寸 的图片不能选中
  57. if (![[TZImageManager manager] isPhotoSelectableWithAsset:model.asset]) {
  58. if (_selectImageView.hidden == NO) {
  59. self.selectPhotoButton.hidden = YES;
  60. _selectImageView.hidden = YES;
  61. }
  62. }
  63. // 如果用户选中了该图片,提前获取一下大图
  64. if (model.isSelected) {
  65. [self fetchBigImage];
  66. }
  67. }
  68. - (void)setShowSelectBtn:(BOOL)showSelectBtn {
  69. _showSelectBtn = showSelectBtn;
  70. if (!self.selectPhotoButton.hidden) {
  71. self.selectPhotoButton.hidden = !showSelectBtn;
  72. }
  73. if (!self.selectImageView.hidden) {
  74. self.selectImageView.hidden = !showSelectBtn;
  75. }
  76. }
  77. - (void)setType:(TZAssetCellType)type {
  78. _type = type;
  79. if (type == TZAssetCellTypePhoto || type == TZAssetCellTypeLivePhoto || (type == TZAssetCellTypePhotoGif && !self.allowPickingGif)) {
  80. _selectImageView.hidden = NO;
  81. _selectPhotoButton.hidden = NO;
  82. _bottomView.hidden = YES;
  83. } else { // Video of Gif
  84. _selectImageView.hidden = YES;
  85. _selectPhotoButton.hidden = YES;
  86. _bottomView.hidden = NO;
  87. if (type == TZAssetCellTypeVideo) {
  88. self.timeLength.text = _model.timeLength;
  89. self.videoImgView.hidden = NO;
  90. _timeLength.tz_left = self.videoImgView.tz_right;
  91. _timeLength.textAlignment = NSTextAlignmentRight;
  92. } else {
  93. self.timeLength.text = @"GIF";
  94. self.videoImgView.hidden = YES;
  95. _timeLength.tz_left = 5;
  96. _timeLength.textAlignment = NSTextAlignmentLeft;
  97. }
  98. }
  99. }
  100. - (void)selectPhotoButtonClick:(UIButton *)sender {
  101. if (self.didSelectPhotoBlock) {
  102. self.didSelectPhotoBlock(sender.isSelected);
  103. }
  104. self.selectImageView.image = sender.isSelected ? [UIImage imageNamedFromMyBundle:self.photoSelImageName] : [UIImage imageNamedFromMyBundle:self.photoDefImageName];
  105. if (sender.isSelected) {
  106. [UIView showOscillatoryAnimationWithLayer:_selectImageView.layer type:TZOscillatoryAnimationToBigger];
  107. // 用户选中了该图片,提前获取一下大图
  108. [self fetchBigImage];
  109. } else { // 取消选中,取消大图的获取
  110. if (_bigImageRequestID && _progressView) {
  111. [[PHImageManager defaultManager] cancelImageRequest:_bigImageRequestID];
  112. [self hideProgressView];
  113. }
  114. }
  115. }
  116. - (void)hideProgressView {
  117. self.progressView.hidden = YES;
  118. self.imageView.alpha = 1.0;
  119. }
  120. - (void)fetchBigImage {
  121. _bigImageRequestID = [[TZImageManager manager] getPhotoWithAsset:_model.asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
  122. if (_progressView) {
  123. [self hideProgressView];
  124. }
  125. } progressHandler:^(double progress, NSError *error, BOOL *stop, NSDictionary *info) {
  126. if (_model.isSelected) {
  127. progress = progress > 0.02 ? progress : 0.02;;
  128. self.progressView.progress = progress;
  129. self.progressView.hidden = NO;
  130. self.imageView.alpha = 0.4;
  131. } else {
  132. *stop = YES;
  133. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  134. }
  135. } networkAccessAllowed:YES];
  136. }
  137. #pragma mark - Lazy load
  138. - (UIButton *)selectPhotoButton {
  139. if (_selectPhotoButton == nil) {
  140. UIButton *selectPhotoButton = [[UIButton alloc] init];
  141. selectPhotoButton.frame = CGRectMake(self.tz_width - 44, 0, 44, 44);
  142. [selectPhotoButton addTarget:self action:@selector(selectPhotoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  143. [self.contentView addSubview:selectPhotoButton];
  144. _selectPhotoButton = selectPhotoButton;
  145. }
  146. return _selectPhotoButton;
  147. }
  148. - (UIImageView *)imageView {
  149. if (_imageView == nil) {
  150. UIImageView *imageView = [[UIImageView alloc] init];
  151. imageView.frame = CGRectMake(0, 0, self.tz_width, self.tz_height);
  152. imageView.contentMode = UIViewContentModeScaleAspectFill;
  153. imageView.clipsToBounds = YES;
  154. [self.contentView addSubview:imageView];
  155. _imageView = imageView;
  156. [self.contentView bringSubviewToFront:_selectImageView];
  157. [self.contentView bringSubviewToFront:_bottomView];
  158. }
  159. return _imageView;
  160. }
  161. - (UIImageView *)selectImageView {
  162. if (_selectImageView == nil) {
  163. UIImageView *selectImageView = [[UIImageView alloc] init];
  164. selectImageView.frame = CGRectMake(self.tz_width - 27, 0, 27, 27);
  165. [self.contentView addSubview:selectImageView];
  166. _selectImageView = selectImageView;
  167. }
  168. return _selectImageView;
  169. }
  170. - (UIView *)bottomView {
  171. if (_bottomView == nil) {
  172. UIView *bottomView = [[UIView alloc] init];
  173. bottomView.frame = CGRectMake(0, self.tz_height - 17, self.tz_width, 17);
  174. static NSInteger rgb = 0;
  175. bottomView.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:0.8];
  176. [self.contentView addSubview:bottomView];
  177. _bottomView = bottomView;
  178. }
  179. return _bottomView;
  180. }
  181. - (UIImageView *)videoImgView {
  182. if (_videoImgView == nil) {
  183. UIImageView *videoImgView = [[UIImageView alloc] init];
  184. videoImgView.frame = CGRectMake(8, 0, 17, 17);
  185. [videoImgView setImage:[UIImage imageNamedFromMyBundle:@"VideoSendIcon.png"]];
  186. [self.bottomView addSubview:videoImgView];
  187. _videoImgView = videoImgView;
  188. }
  189. return _videoImgView;
  190. }
  191. - (UILabel *)timeLength {
  192. if (_timeLength == nil) {
  193. UILabel *timeLength = [[UILabel alloc] init];
  194. timeLength.font = [UIFont boldSystemFontOfSize:11];
  195. timeLength.frame = CGRectMake(self.videoImgView.tz_right, 0, self.tz_width - self.videoImgView.tz_right - 5, 17);
  196. timeLength.textColor = [UIColor whiteColor];
  197. timeLength.textAlignment = NSTextAlignmentRight;
  198. [self.bottomView addSubview:timeLength];
  199. _timeLength = timeLength;
  200. }
  201. return _timeLength;
  202. }
  203. - (TZProgressView *)progressView {
  204. if (_progressView == nil) {
  205. _progressView = [[TZProgressView alloc] init];
  206. static CGFloat progressWH = 20;
  207. CGFloat progressXY = (self.tz_width - progressWH) / 2;
  208. _progressView.hidden = YES;
  209. _progressView.frame = CGRectMake(progressXY, progressXY, progressWH, progressWH);
  210. [self addSubview:_progressView];
  211. }
  212. return _progressView;
  213. }
  214. @end
  215. @interface TZAlbumCell ()
  216. @property (weak, nonatomic) UIImageView *posterImageView;
  217. @property (weak, nonatomic) UILabel *titleLabel;
  218. @property (weak, nonatomic) UIImageView *arrowImageView;
  219. @end
  220. @implementation TZAlbumCell
  221. - (void)setModel:(TZAlbumModel *)model {
  222. _model = model;
  223. NSMutableAttributedString *nameString = [[NSMutableAttributedString alloc] initWithString:model.name attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor blackColor]}];
  224. NSAttributedString *countString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" (%zd)",model.count] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor lightGrayColor]}];
  225. [nameString appendAttributedString:countString];
  226. self.titleLabel.attributedText = nameString;
  227. [[TZImageManager manager] getPostImageWithAlbumModel:model completion:^(UIImage *postImage) {
  228. self.posterImageView.image = postImage;
  229. }];
  230. if (model.selectedCount) {
  231. self.selectedCountButton.hidden = NO;
  232. [self.selectedCountButton setTitle:[NSString stringWithFormat:@"%zd",model.selectedCount] forState:UIControlStateNormal];
  233. } else {
  234. self.selectedCountButton.hidden = YES;
  235. }
  236. }
  237. /// For fitting iOS6
  238. - (void)layoutSubviews {
  239. if (iOS7Later) [super layoutSubviews];
  240. _selectedCountButton.frame = CGRectMake(self.tz_width - 24 - 30, 23, 24, 24);
  241. }
  242. - (void)layoutSublayersOfLayer:(CALayer *)layer {
  243. if (iOS7Later) [super layoutSublayersOfLayer:layer];
  244. }
  245. #pragma mark - Lazy load
  246. - (UIImageView *)posterImageView {
  247. if (_posterImageView == nil) {
  248. UIImageView *posterImageView = [[UIImageView alloc] init];
  249. posterImageView.contentMode = UIViewContentModeScaleAspectFill;
  250. posterImageView.clipsToBounds = YES;
  251. posterImageView.frame = CGRectMake(0, 0, 70, 70);
  252. [self.contentView addSubview:posterImageView];
  253. _posterImageView = posterImageView;
  254. }
  255. return _posterImageView;
  256. }
  257. - (UILabel *)titleLabel {
  258. if (_titleLabel == nil) {
  259. UILabel *titleLabel = [[UILabel alloc] init];
  260. titleLabel.font = [UIFont boldSystemFontOfSize:17];
  261. titleLabel.frame = CGRectMake(80, 0, self.tz_width - 80 - 50, self.tz_height);
  262. titleLabel.textColor = [UIColor blackColor];
  263. titleLabel.textAlignment = NSTextAlignmentLeft;
  264. [self.contentView addSubview:titleLabel];
  265. _titleLabel = titleLabel;
  266. }
  267. return _titleLabel;
  268. }
  269. - (UIImageView *)arrowImageView {
  270. if (_arrowImageView == nil) {
  271. UIImageView *arrowImageView = [[UIImageView alloc] init];
  272. CGFloat arrowWH = 15;
  273. arrowImageView.frame = CGRectMake(self.tz_width - arrowWH - 12, 28, arrowWH, arrowWH);
  274. [arrowImageView setImage:[UIImage imageNamedFromMyBundle:@"TableViewArrow.png"]];
  275. [self.contentView addSubview:arrowImageView];
  276. _arrowImageView = arrowImageView;
  277. }
  278. return _arrowImageView;
  279. }
  280. - (UIButton *)selectedCountButton {
  281. if (_selectedCountButton == nil) {
  282. UIButton *selectedCountButton = [[UIButton alloc] init];
  283. selectedCountButton.layer.cornerRadius = 12;
  284. selectedCountButton.clipsToBounds = YES;
  285. selectedCountButton.backgroundColor = [UIColor redColor];
  286. [selectedCountButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  287. selectedCountButton.titleLabel.font = [UIFont systemFontOfSize:15];
  288. [self.contentView addSubview:selectedCountButton];
  289. _selectedCountButton = selectedCountButton;
  290. }
  291. return _selectedCountButton;
  292. }
  293. @end
  294. @implementation TZAssetCameraCell
  295. - (instancetype)initWithFrame:(CGRect)frame {
  296. self = [super initWithFrame:frame];
  297. if (self) {
  298. self.backgroundColor = [UIColor whiteColor];
  299. _imageView = [[UIImageView alloc] init];
  300. _imageView.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.500];
  301. _imageView.contentMode = UIViewContentModeScaleAspectFill;
  302. [self addSubview:_imageView];
  303. self.clipsToBounds = YES;
  304. }
  305. return self;
  306. }
  307. - (void)layoutSubviews {
  308. [super layoutSubviews];
  309. _imageView.frame = self.bounds;
  310. }
  311. @end