TZTestCell.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // TZTestCell.m
  3. // TZImagePickerController
  4. //
  5. // Created by 谭真 on 16/1/3.
  6. // Copyright © 2016年 谭真. All rights reserved.
  7. //
  8. #import "TZTestCell.h"
  9. #import "UIView+Layout.h"
  10. #import <Photos/Photos.h>
  11. #import <AssetsLibrary/AssetsLibrary.h>
  12. #import "TZImagePickerController/TZImagePickerController.h"
  13. @implementation TZTestCell
  14. - (instancetype)initWithFrame:(CGRect)frame {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. self.backgroundColor = [UIColor whiteColor];
  18. _imageView = [[UIImageView alloc] init];
  19. _imageView.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.500];
  20. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  21. [self addSubview:_imageView];
  22. self.clipsToBounds = YES;
  23. _videoImageView = [[UIImageView alloc] init];
  24. _videoImageView.image = [UIImage imageNamedFromMyBundle:@"MMVideoPreviewPlay"];
  25. _videoImageView.contentMode = UIViewContentModeScaleAspectFill;
  26. _videoImageView.hidden = YES;
  27. [self addSubview:_videoImageView];
  28. _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  29. [_deleteBtn setImage:[UIImage imageNamed:@"photo_delete"] forState:UIControlStateNormal];
  30. _deleteBtn.frame = CGRectMake(self.tz_width - 36, 0, 36, 36);
  31. _deleteBtn.imageEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, -10);
  32. _deleteBtn.alpha = 0.6;
  33. [self addSubview:_deleteBtn];
  34. _gifLable = [[UILabel alloc] init];
  35. _gifLable.text = @"GIF";
  36. _gifLable.textColor = [UIColor whiteColor];
  37. _gifLable.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
  38. _gifLable.textAlignment = NSTextAlignmentCenter;
  39. _gifLable.font = [UIFont systemFontOfSize:10];
  40. _gifLable.frame = CGRectMake(self.tz_width - 25, self.tz_height - 14, 25, 14);
  41. [self addSubview:_gifLable];
  42. }
  43. return self;
  44. }
  45. - (void)layoutSubviews {
  46. [super layoutSubviews];
  47. _imageView.frame = self.bounds;
  48. CGFloat width = self.tz_width / 3.0;
  49. _videoImageView.frame = CGRectMake(width, width, width, width);
  50. }
  51. - (void)setAsset:(id)asset {
  52. _asset = asset;
  53. if ([asset isKindOfClass:[PHAsset class]]) {
  54. PHAsset *phAsset = asset;
  55. _videoImageView.hidden = phAsset.mediaType != PHAssetMediaTypeVideo;
  56. _gifLable.hidden = ![[phAsset valueForKey:@"filename"] containsString:@"GIF"];
  57. } else if ([asset isKindOfClass:[ALAsset class]]) {
  58. ALAsset *alAsset = asset;
  59. _videoImageView.hidden = ![[alAsset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo];
  60. _gifLable.hidden = YES;
  61. }
  62. }
  63. - (void)setRow:(NSInteger)row {
  64. _row = row;
  65. _deleteBtn.tag = row;
  66. }
  67. - (UIView *)snapshotView {
  68. UIView *snapshotView = [[UIView alloc]init];
  69. UIView *cellSnapshotView = nil;
  70. if ([self respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)]) {
  71. cellSnapshotView = [self snapshotViewAfterScreenUpdates:NO];
  72. } else {
  73. CGSize size = CGSizeMake(self.bounds.size.width + 20, self.bounds.size.height + 20);
  74. UIGraphicsBeginImageContextWithOptions(size, self.opaque, 0);
  75. [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  76. UIImage * cellSnapshotImage = UIGraphicsGetImageFromCurrentImageContext();
  77. UIGraphicsEndImageContext();
  78. cellSnapshotView = [[UIImageView alloc]initWithImage:cellSnapshotImage];
  79. }
  80. snapshotView.frame = CGRectMake(0, 0, cellSnapshotView.frame.size.width, cellSnapshotView.frame.size.height);
  81. cellSnapshotView.frame = CGRectMake(0, 0, cellSnapshotView.frame.size.width, cellSnapshotView.frame.size.height);
  82. [snapshotView addSubview:cellSnapshotView];
  83. return snapshotView;
  84. }
  85. @end