QMVideoTableCell.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // QMVideoTableCell.m
  3. // IMSDK-OC
  4. //
  5. // Created by HCF on 16/8/11.
  6. // Copyright © 2016年 HCF. All rights reserved.
  7. //
  8. #import "QMVideoTableCell.h"
  9. @interface QMVideoTableCell() {
  10. UIImageView *_videoImageView;
  11. UILabel *_videoName;
  12. UILabel *_videoSize;
  13. UILabel *_videoDate;
  14. }
  15. @end
  16. @implementation QMVideoTableCell
  17. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  18. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  19. if (self) {
  20. [self createUI];
  21. }
  22. return self;
  23. }
  24. -(void)createUI {
  25. self.contentView.backgroundColor = [UIColor whiteColor];
  26. _videoImageView = [[UIImageView alloc] init];
  27. _videoImageView.frame = CGRectMake(20, 10, 60, 60);
  28. _videoImageView.backgroundColor = [UIColor greenColor];
  29. [self.contentView addSubview:_videoImageView];
  30. _videoName = [[UILabel alloc] init];
  31. _videoName.frame = CGRectMake(90, 10, kScreenWidth-150, 20);
  32. _videoName.font = [UIFont systemFontOfSize:16];
  33. _videoName.textColor = [UIColor blackColor];
  34. [self.contentView addSubview:_videoName];
  35. _videoSize = [[UILabel alloc] init];
  36. _videoSize.frame = CGRectMake(90, 30, kScreenWidth-150, 20);
  37. _videoSize.font = [UIFont systemFontOfSize:14];
  38. _videoSize.textColor = [UIColor lightGrayColor];
  39. [self.contentView addSubview:_videoSize];
  40. _videoDate = [[UILabel alloc] init];
  41. _videoDate.frame = CGRectMake(90, 50, kScreenWidth-150, 20);
  42. _videoDate.font = [UIFont systemFontOfSize:14];
  43. _videoDate.textColor = [UIColor lightGrayColor];
  44. [self.contentView addSubview:_videoDate];
  45. self.pickedItemImageView = [[UIImageView alloc] init];
  46. self.pickedItemImageView.frame = CGRectMake(kScreenWidth-50, 25, 30, 30);
  47. self.pickedItemImageView.image = [UIImage imageNamed:@"ic_checkbox_pressed"];
  48. [self.contentView addSubview:self.pickedItemImageView];
  49. }
  50. - (void)setImageAsset:(PHAsset *)imageAsset {
  51. if (imageAsset) {
  52. PHVideoRequestOptions *voptions = [[PHVideoRequestOptions alloc] init];
  53. voptions.version = PHVideoRequestOptionsVersionCurrent;
  54. voptions.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat;
  55. voptions.networkAccessAllowed = YES;
  56. [self.imageManager requestAVAssetForVideo:imageAsset options:voptions resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
  57. if (asset && [asset isKindOfClass:[AVURLAsset class]] && [NSString stringWithFormat:@"%@",((AVURLAsset *)asset).URL].length > 0) {
  58. NSURL *videoURL = [(AVURLAsset *)asset URL];
  59. NSNumber *fileSizeValue = nil;
  60. [videoURL getResourceValue:&fileSizeValue forKey:NSURLFileSizeKey error:nil];
  61. dispatch_async(dispatch_get_main_queue(), ^{
  62. if (fileSizeValue) {
  63. if (fileSizeValue.intValue>1024) {
  64. _videoSize.text = [NSString stringWithFormat:@"%.f MB", (float)fileSizeValue.intValue/1024/1024];
  65. }else {
  66. _videoSize.text = [NSString stringWithFormat:@"%.f KB", (float)fileSizeValue.intValue/1024];
  67. }
  68. }
  69. });
  70. }
  71. NSString *filePath = [info objectForKey:@"PHImageFileSandboxExtensionTokenKey"];
  72. NSArray * array = [filePath componentsSeparatedByString:@"/"];
  73. dispatch_async(dispatch_get_main_queue(), ^{
  74. _videoName.text = array.lastObject;
  75. });
  76. }];
  77. PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
  78. options.synchronous = YES;
  79. options.version = PHImageRequestOptionsVersionCurrent;
  80. options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
  81. options.resizeMode = PHImageRequestOptionsResizeModeExact;
  82. options.networkAccessAllowed = YES;
  83. [self.imageManager requestImageForAsset:imageAsset targetSize:CGSizeMake(120, 120) contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
  84. _videoImageView.image = result;
  85. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  86. formatter.dateFormat = @"MM-dd HH:mm";
  87. _videoDate.text = [formatter stringFromDate:[imageAsset creationDate]];
  88. }];
  89. }
  90. }
  91. - (void)awakeFromNib {
  92. [super awakeFromNib];
  93. // Initialization code
  94. }
  95. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  96. [super setSelected:selected animated:animated];
  97. }
  98. @end