myMediaVC.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // myMediaVC.h.m
  3. //
  4. // Created by flyeagleTang on 14-4-3.
  5. // Copyright (c) 2014年 Reese. All rights reserved.
  6. //
  7. #import "myMediaVC.h"
  8. #import "JXChatViewController.h"
  9. #import "AppDelegate.h"
  10. #import "JXLabel.h"
  11. #import "JXImageView.h"
  12. //#import "JXCell.h"
  13. #import "JXRoomPool.h"
  14. #import "JXTableView.h"
  15. #import "JXNewFriendViewController.h"
  16. #import "menuImageView.h"
  17. #import "JXMediaCell.h"
  18. #import "JXMediaObject.h"
  19. #import "recordVideoViewController.h"
  20. #import "JXCameraVC.h"
  21. #import <photos/PHAssetResource.h>
  22. #import <photos/PHFetchOptions.h>
  23. #import <photos/PHFetchResult.h>
  24. #import <Photos/PHAsset.h>
  25. #import <Photos/PHImageManager.h>
  26. #import <AssetsLibrary/ALAssetsLibrary.h>
  27. #import <AssetsLibrary/ALAssetRepresentation.h>
  28. #define kCameraVideoPath [FileInfo getUUIDFileName:@"mp4"]
  29. @interface myMediaVC ()<JXCameraVCDelegate>
  30. @end
  31. @implementation myMediaVC
  32. @synthesize delegate;
  33. @synthesize didSelect;
  34. - (id)init
  35. {
  36. self = [super init];
  37. if (self) {
  38. self.title = Localized(@"PSMyViewController_MyAtt");
  39. self.heightHeader = JX_SCREEN_TOP;
  40. self.heightFooter = 0;
  41. self.isGotoBack = YES;
  42. //self.view.frame = g_window.bounds;
  43. [self createHeadAndFoot];
  44. self.isShowFooterPull = NO;
  45. // _table.backgroundColor = HEXCOLOR(0xdbdbdb);
  46. NSString *image = THESIMPLESTYLE ? @"im_003_more_button_black" : @"im_003_more_button_normal";
  47. UIButton* btn = [UIFactory createButtonWithImage:image
  48. highlight:nil
  49. target:self
  50. selector:@selector(onAddVideo)];
  51. btn.custom_acceptEventInterval = 1.f;
  52. btn.frame = CGRectMake(JX_SCREEN_WIDTH-15-18, JX_SCREEN_TOP-15-18, 18, 18);
  53. [self.tableHeader addSubview:btn];
  54. _array=[[NSMutableArray alloc]init];
  55. [self getVersionVideo];
  56. if ([[JXMediaObject sharedInstance] fetch].count > 0) {
  57. [_array addObjectsFromArray:[[JXMediaObject sharedInstance] fetch]];
  58. }
  59. }
  60. return self;
  61. }
  62. - (void)viewDidLoad
  63. {
  64. [super viewDidLoad];
  65. }
  66. - (void)viewDidAppear:(BOOL)animated
  67. {
  68. [super viewDidAppear:animated];
  69. // [self scrollToPageUp];
  70. }
  71. - (void)didReceiveMemoryWarning
  72. {
  73. [super didReceiveMemoryWarning];
  74. // Dispose of any resources that can be recreated.
  75. }
  76. #pragma mark ---------tableView协议----------------
  77. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  78. {
  79. return 1;
  80. }
  81. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  82. {
  83. return _array.count;
  84. }
  85. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. JXMediaCell *cell=nil;
  88. NSString* cellName = [NSString stringWithFormat:@"msg_%ld",indexPath.row];
  89. // cell = [tableView dequeueReusableCellWithIdentifier:cellName];
  90. if(cell==nil){
  91. JXMediaObject *p=_array[indexPath.row];
  92. cell = [JXMediaCell alloc];
  93. [_table addToPool:cell];
  94. cell.media = p;
  95. cell.delegate = self;
  96. cell.tag = indexPath.row;
  97. cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
  98. }
  99. return cell;
  100. }
  101. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  102. {
  103. JXMediaObject *p=_array[indexPath.row];
  104. if(![[NSFileManager defaultManager] fileExistsAtPath:p.fileName]){
  105. // [g_App showAlert:Localized(@"JXAlert_NotHaveFile")];
  106. // return;
  107. }
  108. if (delegate && [delegate respondsToSelector:didSelect]) {
  109. // [delegate performSelector:didSelect withObject:p];
  110. [delegate performSelectorOnMainThread:didSelect withObject:p waitUntilDone:NO];
  111. [self actionQuit];
  112. }
  113. }
  114. - (void)dealloc {
  115. // NSLog(@"myMediaVC.dealloc");
  116. // [_array release];
  117. // [super dealloc];
  118. }
  119. -(void)getServerData{
  120. // if ([[JXMediaObject sharedInstance] fetch].count > 0) {
  121. // [_array addObjectsFromArray:[[JXMediaObject sharedInstance] fetch]];
  122. // }
  123. }
  124. -(void)scrollToPageUp{
  125. [self stopLoading];
  126. _refreshCount++;
  127. // [_array release];
  128. [self getServerData];
  129. [_table reloadData];
  130. }
  131. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  132. {
  133. return 120;
  134. }
  135. -(void)onAddVideo{
  136. AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  137. if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
  138. {
  139. [g_server showMsg:Localized(@"JX_CanNotopenCenmar")];
  140. return;
  141. }
  142. // recordVideoViewController * videoRecordVC = [recordVideoViewController alloc];
  143. // videoRecordVC.maxTime = 30;
  144. // videoRecordVC.isReciprocal = NO;
  145. // videoRecordVC.delegate = self;
  146. // videoRecordVC.didRecord = @selector(newVideo:);
  147. // videoRecordVC = [videoRecordVC init];
  148. // [g_window addSubview:videoRecordVC.view];
  149. JXCameraVC *vc = [JXCameraVC alloc];
  150. vc.cameraDelegate = self;
  151. vc.maxTime = 30;
  152. vc.isVideo = YES;
  153. vc = [vc init];
  154. [self presentViewController:vc animated:YES completion:nil];
  155. }
  156. // 視屏錄製回調
  157. - (void)cameraVC:(JXCameraVC *)vc didFinishWithVideoPath:(NSString *)filePath timeLen:(NSInteger)timeLen {
  158. if( ![[NSFileManager defaultManager] fileExistsAtPath:filePath] )
  159. return;
  160. NSString* file = filePath;
  161. JXMediaObject* p = [[JXMediaObject alloc]init];
  162. p.userId = g_server.myself.userId;
  163. p.fileName = file;
  164. p.isVideo = [NSNumber numberWithBool:YES];
  165. p.timeLen = [NSNumber numberWithInteger:timeLen];
  166. [_array insertObject:p atIndex:0];
  167. [p insert];
  168. [_table reloadData];
  169. }
  170. -(void)newVideo:(recordVideoViewController *)sender;
  171. {
  172. if( ![[NSFileManager defaultManager] fileExistsAtPath:sender.outputFileName] )
  173. return;
  174. NSString* file = sender.outputFileName;
  175. JXMediaObject* p = [[JXMediaObject alloc]init];
  176. p.userId = g_server.myself.userId;
  177. p.fileName = file;
  178. p.isVideo = [NSNumber numberWithBool:YES];
  179. p.timeLen = [NSNumber numberWithInt:sender.timeLen];
  180. [p insert];
  181. // [p release];
  182. [self scrollToPageUp];
  183. }
  184. - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  185. {
  186. JXMediaObject *p=_array[indexPath.row];
  187. [p delete];
  188. p = nil;
  189. [_array removeObjectAtIndex:indexPath.row];
  190. _refreshCount++;
  191. [_table reloadData];
  192. }
  193. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  194. return YES;
  195. }
  196. #pragma mark - 获取本地视频资源
  197. - (void)getVersionVideo {
  198. PHFetchOptions *options = [[PHFetchOptions alloc] init];
  199. options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
  200. PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
  201. // 这时 assetsFetchResults 中包含的,应该就是各个资源(PHAsset)
  202. for (NSInteger i = 0; i < assetsFetchResults.count; i++) {
  203. // 获取一个资源(PHAsset)
  204. PHAsset *phAsset = assetsFetchResults[i];
  205. if (phAsset.mediaType == PHAssetMediaTypeVideo) {
  206. PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  207. options.version = PHImageRequestOptionsVersionCurrent;
  208. options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
  209. PHImageManager *manager = [PHImageManager defaultManager];
  210. [manager requestAVAssetForVideo:phAsset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
  211. dispatch_async(dispatch_get_main_queue(), ^{
  212. AVURLAsset *urlAsset = (AVURLAsset *)asset;
  213. //获取视频本地URL
  214. NSURL *url = urlAsset.URL;
  215. //本地URL存在并且没有保存在数据库
  216. if (url && ![[JXMediaObject sharedInstance] haveTheMediaWithPhotoPath:url.absoluteString]) {
  217. // 获取视频data
  218. NSData *data = [NSData dataWithContentsOfURL:url];
  219. //获取视频拍摄时间
  220. NSDate *date = [self getAudioCreatDate:url];
  221. //新建一个路径并写入视频data
  222. NSString *dataPath = kCameraVideoPath;
  223. [data writeToFile:dataPath atomically:YES];
  224. // 获取视频时长
  225. AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:nil];
  226. NSInteger second = 0;
  227. second = (NSInteger)urlAsset.duration.value / urlAsset.duration.timescale;
  228. // 主线程更新界面
  229. JXMediaObject* p = [[JXMediaObject alloc] init];
  230. p.userId = MY_USER_ID;
  231. p.fileName = dataPath;
  232. p.isVideo = [NSNumber numberWithBool:YES];
  233. p.timeLen = [NSNumber numberWithInteger:second];
  234. p.createTime = date;
  235. p.photoPath = url.absoluteString;
  236. [p insert];
  237. [_array insertObject:p atIndex:0];
  238. [_table reloadData];
  239. }
  240. });
  241. }];
  242. }
  243. }
  244. }
  245. - (NSDate *)getAudioCreatDate:(NSURL*)URL {
  246. NSDate *creatDate;
  247. NSFileManager *fm = [NSFileManager defaultManager];
  248. NSDictionary *fileAttributes = [fm attributesOfItemAtPath:URL.path error:nil];
  249. if (fileAttributes) {
  250. if ((creatDate = [fileAttributes objectForKey:NSFileCreationDate])) {
  251. NSLog(@"date = %@",creatDate);
  252. return creatDate;
  253. }
  254. }
  255. return nil;
  256. }
  257. @end