LogAddViewController.m 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. //
  2. // LogAddViewController.m
  3. // IBOSSmini
  4. //
  5. // Created by guan hong hou on 2017/5/9.
  6. // Copyright © 2017年 elongtian. All rights reserved.
  7. //
  8. #define logTextFont [UIFont systemFontOfSize:14]
  9. #import "LogAddViewController.h"
  10. #import "DateFormat.h"
  11. #import "LogShareVC.h"
  12. #import "LogCommentVC.h"
  13. #import "LogNewCollectionViewCell.h"
  14. #import "MapViewController.h"
  15. #import <MobileCoreServices/MobileCoreServices.h>
  16. #import "DKAudioPlayerHelper.h"
  17. #import "LeslieAsyncImageDownloader.h"
  18. @interface LogAddViewController ()
  19. @end
  20. @implementation LogAddViewController{
  21. UITabBar* bar;
  22. UIView * contentView;
  23. UIView *vPositionView;
  24. UIScrollView *scroll;
  25. }
  26. @synthesize shareArr=_shareArr;
  27. @synthesize commentArr=_commentArr;
  28. #pragma mark - 公共函数
  29. /**
  30. viewDidLoad函数
  31. */
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.navigationItem.title=@"新增日志";
  35. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  36. [button setBackgroundImage:[UIImage imageNamed:@"icon_back.png"]
  37. forState:UIControlStateNormal];
  38. [button addTarget:self action:@selector(goBack)
  39. forControlEvents:UIControlEventTouchUpInside];
  40. button.frame = CGRectMake(0, 0, 15, 18);
  41. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  42. self.navigationItem.leftBarButtonItem = menuButton;
  43. [self initUI];
  44. _allowsSendVoice = YES;
  45. self.delegate = self;
  46. self.dataSource = self;
  47. _sDate= [DateFormat getCurrentDateTime];
  48. [_btnDate setTitle:[DateFormat dateFormatSplit:_sDate]forState:UIControlStateNormal];
  49. }
  50. /**
  51. 安全区视图发生变化
  52. */
  53. -(void)viewSafeAreaInsetsDidChange{
  54. scroll.frame = CGRectMake(0, 0, SCREENWIDTH, self.view.safeAreaLayoutGuide.layoutFrame.size.height- 45.0);
  55. CGRect inputFrame = CGRectMake(0.0f,
  56. self.view.safeAreaLayoutGuide.layoutFrame.size.height - 45.0,
  57. self.view.safeAreaLayoutGuide.layoutFrame.size.width,
  58. 45.0);
  59. _inputView.frame = inputFrame;
  60. [super viewSafeAreaInsetsDidChange];
  61. }
  62. //获得DKVoiceRecordHUD
  63. - (DKVoiceRecordHUD *)voiceRecordHUD {
  64. if (!_voiceRecordHUD) {
  65. _voiceRecordHUD = [[DKVoiceRecordHUD alloc] initWithFrame:CGRectMake(0, 0, 140, 140)];
  66. }
  67. return _voiceRecordHUD;
  68. }
  69. //获得DKVoiceRecordHelper
  70. - (DKVoiceRecordHelper *)voiceRecordHelper {
  71. if (!_voiceRecordHelper) {
  72. _isMaxTimeStop = NO;
  73. WEAKSELF
  74. _voiceRecordHelper = [[DKVoiceRecordHelper alloc] init];
  75. _voiceRecordHelper.maxTimeStopRecorderCompletion = ^{
  76. UIButton *holdDown = weakSelf.inputView.holdDownButton;
  77. holdDown.selected = NO;
  78. holdDown.highlighted = NO;
  79. weakSelf.isMaxTimeStop = YES;
  80. [weakSelf finishRecorded];
  81. };
  82. _voiceRecordHelper.peakPowerForChannel = ^(float peakPowerForChannel) {
  83. weakSelf.voiceRecordHUD.peakPower = peakPowerForChannel;
  84. };
  85. _voiceRecordHelper.maxRecordTime = kVoiceRecorderTotalTime;
  86. }
  87. return _voiceRecordHelper;
  88. }
  89. //手机屏幕方向方法
  90. #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
  91. - (NSUInteger)supportedInterfaceOrientations
  92. #else
  93. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  94. #endif
  95. {
  96. return UIInterfaceOrientationMaskPortrait;
  97. }
  98. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  99. return UIInterfaceOrientationPortrait;
  100. }
  101. //相册数组属性
  102. - (NSMutableArray *)assets{
  103. if (!_assets) {
  104. _assets = [NSMutableArray array];
  105. }
  106. return _assets;
  107. }
  108. //图片路径数组
  109. - (NSMutableArray *)imagePaths{
  110. if (!_imagePaths) {
  111. _imagePaths = [NSMutableArray array];
  112. }
  113. return _imagePaths;
  114. }
  115. //声音路径数组
  116. - (NSMutableArray *)voicePaths{
  117. if (!_voicePaths) {
  118. _voicePaths = [NSMutableArray array];
  119. }
  120. return _voicePaths;
  121. }
  122. //声音上传路径数组
  123. - (NSMutableArray *)voiceUploadPaths{
  124. if (!_voiceUploadPaths) {
  125. _voiceUploadPaths = [NSMutableArray array];
  126. }
  127. return _voiceUploadPaths;
  128. }
  129. //声音上传时间数组
  130. -(NSMutableArray *)voiceUploadTimes{
  131. if (!_voiceUploadTimes) {
  132. _voiceUploadTimes = [NSMutableArray array];
  133. }
  134. return _voiceUploadTimes;
  135. }
  136. //图片本地路径数组
  137. -(NSMutableArray *)imageLocalPaths{
  138. if (!_imageLocalPaths) {
  139. _imageLocalPaths = [NSMutableArray array];
  140. }
  141. return _imageLocalPaths;
  142. }
  143. // 播放语音方法
  144. - (void)multiMediaMessageDidSelectedOnMessage:(id<DKMessageModel>)message atIndexPath:(NSIndexPath *)indexPath onMessageTableViewCell:(DKUITableViewCell *)messageTableViewCell {
  145. UIViewController *disPlayViewController;
  146. switch (message.messageMediaType) {
  147. case DKBubbleMessageMediaTypeVoice: {
  148. message.isRead = YES;
  149. messageTableViewCell.messageBubbleView.voiceUnreadDotImageView.hidden = YES;
  150. [[DKAudioPlayerHelper shareInstance] setDelegate:(id<NSFileManagerDelegate>)self];
  151. if (_currentSelectedCell) {
  152. [_currentSelectedCell.messageBubbleView.animationVoiceImageView stopAnimating];
  153. }
  154. if (_currentSelectedCell == messageTableViewCell) {
  155. [messageTableViewCell.messageBubbleView.animationVoiceImageView stopAnimating];
  156. [[DKAudioPlayerHelper shareInstance] stopAudio];
  157. self.currentSelectedCell = nil;
  158. } else {
  159. self.currentSelectedCell = messageTableViewCell;
  160. [messageTableViewCell.messageBubbleView.animationVoiceImageView startAnimating];
  161. [[DKAudioPlayerHelper shareInstance] managerAudioWithFileName:message.voicePath toPlay:YES];
  162. }
  163. break;
  164. }
  165. default:
  166. break;
  167. }
  168. if (disPlayViewController) {
  169. [self.navigationController pushViewController:disPlayViewController animated:YES];
  170. }
  171. }
  172. //停止播放语音方法
  173. - (void)didAudioPlayerStopPlay:(AVAudioPlayer *)audioPlayer {
  174. if (!_currentSelectedCell) {
  175. return;
  176. }
  177. [_currentSelectedCell.messageBubbleView.animationVoiceImageView stopAnimating];
  178. self.currentSelectedCell = nil;
  179. }
  180. /**
  181. * 发送语音消息的回调方法
  182. *
  183. * @param voicePath 目标语音本地路径
  184. * @param voiceDuration 目标语音时长
  185. * @param sender 发送者的名字
  186. * @param date 发送时间
  187. */
  188. - (void)didSendVoice:(NSString *)voicePath voiceDuration:(NSString *)voiceDuration fromSender:(NSString *)sender onDate:(NSDate *)date {
  189. int value=ceilf([voiceDuration floatValue]);
  190. NSString* duration=[NSString stringWithFormat:@"%d",value ] ;
  191. DKMessage *voiceMessage = [[DKMessage alloc] initWithVoicePath:voicePath voiceUrl:nil voiceDuration:duration sender:sender timestamp:date];
  192. [self.voicePaths addObject:voicePath];
  193. [self.voiceUploadTimes addObject:duration];
  194. [self addMessage:voiceMessage];
  195. CGFloat collectionheight=0;
  196. if (self.assets.count==0) {
  197. collectionheight=0;
  198. }else {
  199. collectionheight= [self getPhotoCollectionViewHeightWithPhotos:self.assets];
  200. }
  201. _collectionView.frame=CGRectMake(10,CGRectGetMaxY(_txtContent.frame)+8, Screen_Width-20, collectionheight);
  202. _positionImg.frame=CGRectMake(15, CGRectGetMaxY(_collectionView.frame)+15,9,12);
  203. _lblPosition.frame=CGRectMake(CGRectGetMaxX(_positionImg.frame)+11,CGRectGetMaxY(_collectionView.frame)+10, 350, 20);
  204. _btnTakePhoto.frame=CGRectMake(15, CGRectGetMaxY(_positionImg.frame)+15,80,20);
  205. _btnPhoto.frame=CGRectMake((Screen_Width/3)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20);
  206. _btnPosition.frame=CGRectMake((Screen_Width/3*2)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20);
  207. _separatorView.frame=CGRectMake(15, CGRectGetMaxY(_btnTakePhoto.frame)+15,Screen_Width-15, 1);
  208. _voiceImg.frame=CGRectMake(17,CGRectGetMaxY(_separatorView.frame)+18, 13, 15);
  209. _lblVoice.frame=CGRectMake(CGRectGetMaxX(_voiceImg.frame)+13,CGRectGetMaxY(_separatorView.frame)+15,30, 20);
  210. _btnRecord.frame=CGRectMake(Screen_Width-90, CGRectGetMaxY(_separatorView.frame)+18,100,20);
  211. _messageTV.frame=CGRectMake(CGRectGetMaxX(_lblVoice.frame)+2, CGRectGetMaxY(_separatorView.frame)+18,190, [self getSoundViewHeight:self.voicePaths]);
  212. // _messageTV.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  213. if(self.voicePaths.count>0){
  214. contentView.frame=CGRectMake(0, 0, Screen_Width,CGRectGetMaxY(_messageTV.frame));
  215. }
  216. else{
  217. contentView.frame=CGRectMake(0, 0, Screen_Width,CGRectGetMaxY(_btnRecord.frame));
  218. }
  219. scroll.contentSize=CGSizeMake(self.view.frame.size.width, contentView.frame.size.height+110);
  220. }
  221. // DKMessageTableViewController Delegate
  222. - (BOOL)shouldPreventScrollToBottomWhileUserScrolling {
  223. return YES;
  224. }
  225. //DKMessageTableViewController DataSource
  226. - (id <DKMessageModel>)messageForRowAtIndexPath:(NSIndexPath *)indexPath {
  227. return self.messages[indexPath.row];
  228. }
  229. // DKMessageInputView Delegate
  230. - (void)didChangeSendVoiceAction:(BOOL)changed {
  231. if (changed) {
  232. if (self.textViewInputViewType == DKInputViewTypeText)
  233. return;
  234. [self layoutOtherMenuViewHiden:YES];
  235. }
  236. }
  237. // scrollView delegate开始拖拽方法
  238. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  239. self.isUserScrolling = YES;
  240. UIMenuController *menu = [UIMenuController sharedMenuController];
  241. if (menu.isMenuVisible) {
  242. [menu setMenuVisible:NO animated:YES];
  243. }
  244. if (self.textViewInputViewType != DKInputViewTypeNormal) {
  245. // [self layoutOtherMenuViewHiden:YES];
  246. }
  247. }
  248. /*
  249. scrollView delegate结束拖拽方法
  250. */
  251. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  252. self.isUserScrolling = NO;
  253. }
  254. //点击位置后回调函数
  255. -(void)relocationData:(NSString *)name{
  256. self.sLocation=name;
  257. if (self.sLocation.length>0){
  258. _lblPosition.text=self.sLocation;
  259. }
  260. }
  261. // 保存完成后更新画面
  262. - (void)onLoadFinish:(ASIDownManager *)sender {
  263. NSDictionary *dic = [sender.mWebStr JSONValue];
  264. [self cancel];
  265. if (dic && [dic isKindOfClass:[NSDictionary class]]) {
  266. int iStatus = [[dic objectForKey:@"Status"] intValue];
  267. NSString *message=[dic objectForKey:@"Message"];
  268. self.navigationItem.rightBarButtonItem.enabled=YES;
  269. if (iStatus == 0) {
  270. [self clearSenderData];
  271. [self showAlertViewText:@"保存成功!"];
  272. }
  273. else if(iStatus==ActionResultStatusAuthError
  274. ||iStatus==ActionResultStatusNoLogin
  275. ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){
  276. [self showReLoginDialog:message];
  277. return;
  278. }
  279. else {
  280. [self showAlertViewText:message];
  281. }
  282. }
  283. }
  284. // 保存失败
  285. - (void)onLoadFail:(ASIDownManager *)sender {
  286. [self cancel];
  287. self.navigationItem.rightBarButtonItem.enabled=YES;
  288. [self showAlertViewText:@"操作失败"];
  289. }
  290. //开始编辑UITextField的方法 开始编辑UITextField的方法
  291. -(void)textViewDidBeginEditing:(UITextView *)textView
  292. {
  293. CGRect textFrame = textView.frame;
  294. float textY = textFrame.origin.y+textFrame.size.height;
  295. float bottomY = self.view.frame.size.height-textY;
  296. if(bottomY>=216) //判断当前的高度是否已经有216,如果超过了就不需要再移动主界面的View高度
  297. {
  298. _preTag = -1;
  299. return;
  300. }
  301. _preTag = textView.tag;
  302. float moveY = 216-bottomY;
  303. _preMoveY = moveY;
  304. NSTimeInterval animationDuration = 0.30f;
  305. CGRect frame = self.view.frame;
  306. frame.origin.y -=moveY;//view的Y轴上移
  307. frame.size.height +=moveY; //View的高度增加
  308. self.view.frame = frame;
  309. [UIView beginAnimations:@"ResizeView" context:nil];
  310. [UIView setAnimationDuration:animationDuration];
  311. self.view.frame = frame;
  312. [UIView commitAnimations];//设置调整界面的动画效果
  313. }
  314. /**
  315. 结束编辑UITextField的方法,让原来的界面还原高度
  316. */
  317. -(void)textViewDidEndEditing:(UITextView *)textView
  318. {
  319. if(_preTag == -1) //当编辑的View不是需要移动的View
  320. {
  321. return;
  322. }
  323. float moveY ;
  324. NSTimeInterval animationDuration = 0.30f;
  325. CGRect frame = self.view.frame;
  326. if(_preTag == textView.tag) //当结束编辑的View的TAG是上次的就移动
  327. { //还原界面
  328. moveY = _preMoveY;
  329. frame.origin.y +=moveY;
  330. frame.size. height -=moveY;
  331. self.view.frame = frame;
  332. }
  333. //self.view移回原位置
  334. [UIView beginAnimations:@"ResizeView" context:nil];
  335. [UIView setAnimationDuration:animationDuration];
  336. self.view.frame = frame;
  337. [UIView commitAnimations];
  338. [textView resignFirstResponder];
  339. }
  340. //Table View 数据源-得到分区数
  341. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  342. return 1;
  343. }
  344. //Table View 数据源-每个分区的行数
  345. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  346. return self.messages.count;
  347. }
  348. //Table View 数据源-获得tableviewcell
  349. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  350. id <DKMessageModel> message = [self.dataSource messageForRowAtIndexPath:indexPath];
  351. // 如果需要定制复杂的业务UI,那么就实现该DataSource方法
  352. if ([self.dataSource respondsToSelector:@selector(tableView:cellForRowAtIndexPath:targetMessage:)]) {
  353. UITableViewCell *tableViewCell = [self.dataSource tableView:tableView cellForRowAtIndexPath:indexPath targetMessage:message];
  354. return tableViewCell;
  355. }
  356. BOOL displayTimestamp = NO;
  357. static NSString *cellIdentifier = @"DKUITableViewCell";
  358. DKUITableViewCell *messageTableViewCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  359. if (!messageTableViewCell) {
  360. messageTableViewCell = [[DKUITableViewCell alloc] initWithMessage:message displaysTimestamp:displayTimestamp reuseIdentifier:cellIdentifier];
  361. messageTableViewCell.delegate = self;
  362. }
  363. messageTableViewCell.indexPath = indexPath;
  364. [messageTableViewCell configureCellWithMessage:message displaysTimestamp:displayTimestamp];
  365. [messageTableViewCell setBackgroundColor:tableView.backgroundColor];
  366. return messageTableViewCell;
  367. }
  368. //获得tableview的高度
  369. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  370. id <DKMessageModel> messageModel = [self.dataSource messageForRowAtIndexPath:indexPath];
  371. CGFloat calculateCellHeight ;
  372. if ([self.delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:targetMessage:)]) {
  373. calculateCellHeight = [self.delegate tableView:tableView heightForRowAtIndexPath:indexPath targetMessage:messageModel];
  374. } else {
  375. calculateCellHeight = [self calculateCellHeightWithMessage:messageModel atIndexPath:indexPath];
  376. }
  377. return calculateCellHeight;
  378. }
  379. // UICollectionViewDataSource
  380. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  381. return 1;
  382. }
  383. //DKPhotoPickerBrowserViewControllerDataSource
  384. - (NSInteger)numberOfSectionInPhotosInPickerBrowser:(DKPhotoPickerBrowserViewController *)pickerBrowser{
  385. return self.assets.count;
  386. }
  387. //DKPhotoPickerBrowserViewControllerDataSource
  388. - (NSInteger)photoBrowser:(DKPhotoPickerBrowserViewController *)photoBrowser numberOfItemsInSection:(NSUInteger)section{
  389. return [self.assets count];
  390. }
  391. //DKPhotoPickerBrowserViewControllerDataSource
  392. - (DKPhotoPickerBrowserPhoto *)photoBrowser:(DKPhotoPickerBrowserViewController *)pickerBrowser photoAtIndexPath:(NSIndexPath *)indexPath{
  393. id imageObj = [self.assets objectAtIndex:indexPath.item];
  394. DKPhotoPickerBrowserPhoto *photo = [DKPhotoPickerBrowserPhoto photoAnyImageObjWith:imageObj];
  395. // 包装下imageObj 成 DKPhotoPickerBrowserPhoto 传给数据源
  396. LogNewCollectionViewCell *cell = (LogNewCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
  397. // 缩略图
  398. photo.thumbImage = cell.imageView.image;
  399. return photo;
  400. }
  401. //DKPhotoPickerBrowserViewControllerDelegate
  402. - (void)photoBrowser:(DKPhotoPickerBrowserViewController *)photoBrowser removePhotoAtIndexPath:(NSIndexPath *)indexPath{
  403. if (indexPath.row > [self.assets count]) return;
  404. [self.assets removeObjectAtIndex:indexPath.row];
  405. [self.collectionView reloadData];
  406. }
  407. //collectionview datasource
  408. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  409. CGFloat collectionheight=0;
  410. if (self.assets.count==0) {
  411. collectionheight=0;
  412. }else {
  413. collectionheight= [self getPhotoCollectionViewHeightWithPhotos:self.assets];
  414. }
  415. _collectionView.frame=CGRectMake(10,CGRectGetMaxY(_txtContent.frame)+8, Screen_Width-20, collectionheight);
  416. _positionImg.frame=CGRectMake(15, CGRectGetMaxY(_collectionView.frame)+15,9,12);
  417. _lblPosition.frame=CGRectMake(CGRectGetMaxX(_positionImg.frame)+11,CGRectGetMaxY(_collectionView.frame)+10, 350, 20);
  418. _btnTakePhoto.frame=CGRectMake(15, CGRectGetMaxY(_positionImg.frame)+15,80,20);
  419. _btnPhoto.frame=CGRectMake((Screen_Width/3)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20);
  420. _btnPosition.frame=CGRectMake((Screen_Width/3*2)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20);
  421. _separatorView.frame=CGRectMake(15, CGRectGetMaxY(_btnTakePhoto.frame)+15,Screen_Width-15,1);
  422. _voiceImg.frame=CGRectMake(17,CGRectGetMaxY(_separatorView.frame)+18, 13, 15);
  423. _lblVoice.frame=CGRectMake(CGRectGetMaxX(_voiceImg.frame)+13,CGRectGetMaxY(_separatorView.frame)+15,30, 20);
  424. _btnRecord.frame=CGRectMake(Screen_Width-90, CGRectGetMaxY(_separatorView.frame)+18,100,20);
  425. _messageTV.frame=CGRectMake(CGRectGetMaxX(_lblVoice.frame)+2, CGRectGetMaxY(_separatorView.frame)+18,140, [self getSoundViewHeight:self.voicePaths]);
  426. if(self.voicePaths.count>0){
  427. contentView.frame=CGRectMake(0, 0, Screen_Width,CGRectGetMaxY( _messageTV.frame));
  428. }
  429. else{
  430. contentView.frame=CGRectMake(0, 0, Screen_Width,CGRectGetMaxY(_btnRecord.frame));
  431. }
  432. scroll.contentSize=CGSizeMake(self.view.frame.size.width, contentView.frame.size.height+110);
  433. return [self.assets count];
  434. }
  435. //collectionview datasource
  436. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  437. LogNewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LogNewCollectionViewCell" forIndexPath:indexPath];
  438. // 判断类型来获取Image
  439. DKPhotoAssets *asset = self.assets[indexPath.item];
  440. if ([asset isKindOfClass:[DKPhotoAssets class]]) {
  441. cell.imageView.image = asset.originImage;
  442. }else if ([asset isKindOfClass:[NSString class]]){
  443. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:(NSString *)asset] placeholderImage:[UIImage imageNamed:@"wallpaper_placeholder"]];
  444. }else if([asset isKindOfClass:[UIImage class]]){
  445. cell.imageView.image = (UIImage *)asset;
  446. }else if ([asset isKindOfClass:[DKCamera class]]){
  447. cell.imageView.image = [(DKCamera*)asset thumbImage];
  448. }
  449. return cell;
  450. }
  451. //collectionView Delegate
  452. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  453. LogNewCollectionViewCell *cell = (LogNewCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
  454. // 图片游览器
  455. DKPhotoPickerBrowserViewController *pickerBrowser = [[DKPhotoPickerBrowserViewController alloc] init];
  456. // 传入点击图片View的话,会有微信朋友圈照片的风格
  457. pickerBrowser.toView = cell.imageView;
  458. // 数据源/delegate
  459. pickerBrowser.delegate = self;
  460. pickerBrowser.dataSource = self;
  461. // 是否可以删除照片
  462. pickerBrowser.editing = YES;
  463. // 当前选中的值
  464. pickerBrowser.currentIndexPath = [NSIndexPath indexPathForItem:indexPath.row inSection:0];
  465. // 展示控制器
  466. [pickerBrowser show];
  467. }
  468. //分享事件回调函数
  469. - (void)reShareData:(NSArray *)sarray
  470. {
  471. if(self.shareArr==nil){
  472. self.shareArr=[NSMutableArray new];
  473. }
  474. [self.shareArr removeAllObjects];
  475. [self.shareArr addObjectsFromArray:sarray];
  476. NSMutableArray* ids=[NSMutableArray new];
  477. NSMutableArray* names=[NSMutableArray new];
  478. for (int i=0; i<self.shareArr.count; i++) {
  479. LogShareAndCommentInfoModel* model=self.shareArr[i];
  480. [names addObject:model.userName];
  481. [ids addObject:model.userId];
  482. }
  483. if(self.shareArr!=nil && self.shareArr.count>0){
  484. self.sShareId=[ ids componentsJoinedByString:@","];
  485. self.sShareUserName=[names componentsJoinedByString:@","];
  486. [_btnShare setTitle:self.sShareUserName forState:UIControlStateNormal];
  487. [_btnComment setTitle:@"请选择点评人" forState:UIControlStateNormal];
  488. self.sCommentId = @"";
  489. self.sComment = @"";
  490. }
  491. }
  492. // 评论事件回调函数
  493. - (void)reCommentData:(LogShareAndCommentInfoModel*)model
  494. {
  495. if(self.commentArr==nil){
  496. self.commentArr=[NSMutableArray new];
  497. }
  498. [self.commentArr removeAllObjects];
  499. [self.commentArr addObject:model ];
  500. NSMutableArray* ids=[NSMutableArray new];
  501. NSMutableArray* names=[NSMutableArray new];
  502. for (int i=0; i<self.commentArr.count; i++) {
  503. LogShareAndCommentInfoModel* model=self.commentArr[i];
  504. [names addObject:model.userName];
  505. [ids addObject:model.userId];
  506. }
  507. if(self.commentArr!=nil && self.commentArr.count>0){
  508. self.sCommentId=[ ids componentsJoinedByString:@","];
  509. self.sComment=[names componentsJoinedByString:@","];
  510. [_btnComment setTitle:self.sComment forState:UIControlStateNormal];
  511. }
  512. }
  513. //照片选择完回调方法
  514. - (void)pickerViewControllerDoneAsstes:(NSArray *)assets{
  515. for (int i=(int)self.assets.count-1;i>=0;i-- ) {
  516. if ([self.assets[i] isKindOfClass:[DKPhotoAssets class]]) {
  517. [self.assets removeObject:self.assets[i]];
  518. }
  519. }
  520. for (DKPhotoAssets* asset in assets ) {
  521. if ([asset isKindOfClass:[DKPhotoAssets class]]) {
  522. [self.assets addObject:asset];
  523. }
  524. }
  525. [self.collectionView reloadData];
  526. }
  527. #pragma mark - 私有方法
  528. //获得录音路径
  529. - (NSString *)getRecorderPath {
  530. NSString *recorderPath = nil;
  531. recorderPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
  532. NSDate *now = [NSDate date];
  533. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  534. [dateFormatter setDateFormat:@"yyyyMMddHHmmssSSS"];
  535. recorderPath = [recorderPath stringByAppendingFormat:@"%@-MySound.aac", [dateFormatter stringFromDate:now]];
  536. return recorderPath;
  537. }
  538. //tableview底部间距
  539. - (UIEdgeInsets)tableViewInsetsWithBottomValue:(CGFloat)bottom {
  540. UIEdgeInsets insets = UIEdgeInsetsZero;
  541. if ([self respondsToSelector:@selector(topLayoutGuide)]) {
  542. insets.top = 2;//self.topLayoutGuide.length;
  543. }
  544. insets.bottom = bottom;
  545. return insets;
  546. }
  547. // 计算消息的高度
  548. - (CGFloat)calculateCellHeightWithMessage:(id <DKMessageModel>)message atIndexPath:(NSIndexPath *)indexPath {
  549. CGFloat cellHeight = 0;
  550. cellHeight = [DKUITableViewCell calculateCellHeightWithMessage:message displaysTimestamp:NO];
  551. return cellHeight;
  552. }
  553. //发送语音方法
  554. - (void)didSendMessageWithVoice:(NSString *)voicePath voiceDuration:(NSString*)voiceDuration {
  555. if ([self.delegate respondsToSelector:@selector(didSendVoice:voiceDuration:fromSender:onDate:)]) {
  556. [self.delegate didSendVoice:voicePath voiceDuration:voiceDuration fromSender:self.messageSender onDate:[NSDate date]];
  557. }
  558. }
  559. // Other Menu View Frame Helper Mehtod
  560. - (void)layoutOtherMenuViewHiden:(BOOL)hide {
  561. [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
  562. __block CGRect inputViewFrame = _inputView.frame;
  563. __block CGRect otherMenuViewFrame;
  564. void (^InputViewAnimation)(BOOL hide) = ^(BOOL hide) {
  565. inputViewFrame.origin.y = (hide ? (CGRectGetHeight(self.view.bounds) - CGRectGetHeight(inputViewFrame)) : (CGRectGetMinY(otherMenuViewFrame) - CGRectGetHeight(inputViewFrame)));
  566. _inputView.frame = inputViewFrame;
  567. };
  568. InputViewAnimation(hide);
  569. } completion:^(BOOL finished) {
  570. }];
  571. }
  572. //录音完成方法
  573. - (void)prepareRecordWithCompletion:(DKPrepareRecorderCompletion)completion {
  574. [self.voiceRecordHelper prepareRecordingWithPath:[self getRecorderPath] prepareRecorderCompletion:completion];
  575. }
  576. //开始录音方法
  577. - (void)startRecord {
  578. [self.voiceRecordHUD startRecordingHUDAtView:self.view];
  579. [self.voiceRecordHelper startRecordingWithStartRecorderCompletion:^{
  580. }];
  581. }
  582. //完成录音方法
  583. - (void)finishRecorded {
  584. WEAKSELF
  585. [self.voiceRecordHUD stopRecordCompled:^(BOOL fnished) {
  586. weakSelf.voiceRecordHUD = nil;
  587. }];
  588. [self.voiceRecordHelper stopRecordingWithStopRecorderCompletion:^{
  589. [weakSelf didSendMessageWithVoice:weakSelf.voiceRecordHelper.recordPath voiceDuration:weakSelf.voiceRecordHelper.recordDuration];
  590. }];
  591. }
  592. /**
  593. * 改变数据源需要的子线程
  594. *
  595. * @param queue 子线程执行完成的回调block
  596. */
  597. - (void)exChangeMessageDataSourceQueue:(void (^)())queue {
  598. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), queue);
  599. }
  600. /**
  601. * 执行块代码在主线程
  602. *
  603. * @param queue 主线程执行完成回调block
  604. */
  605. - (void)exMainQueue:(void (^)())queue {
  606. dispatch_async(dispatch_get_main_queue(), queue);
  607. }
  608. //停止录音方法
  609. - (void)pauseRecord {
  610. [self.voiceRecordHUD pauseRecord];
  611. }
  612. //恢复录音方法
  613. - (void)resumeRecord {
  614. [self.voiceRecordHUD resaueRecord];
  615. }
  616. //取消录音方法
  617. - (void)cancelRecord {
  618. WEAKSELF
  619. [self.voiceRecordHUD cancelRecordCompled:^(BOOL fnished) {
  620. weakSelf.voiceRecordHUD = nil;
  621. }];
  622. [self.voiceRecordHelper cancelledDeleteWithCompletion:^{
  623. }];
  624. }
  625. //准备录音完成方法
  626. - (void)prepareRecordingVoiceActionWithCompletion:(BOOL (^)(void))completion {
  627. [self prepareRecordWithCompletion:completion];
  628. }
  629. //开始录音事件方法
  630. - (void)didStartRecordingVoiceAction {
  631. [self startRecord];
  632. }
  633. //取消录音事件方法
  634. - (void)didCancelRecordingVoiceAction {
  635. [self cancelRecord];
  636. }
  637. //完成录音事件方法
  638. - (void)didFinishRecordingVoiceAction {
  639. if (self.isMaxTimeStop == NO) {
  640. [self finishRecorded];
  641. } else {
  642. self.isMaxTimeStop = NO;
  643. }
  644. }
  645. //手指移出事件
  646. - (void)didDragOutsideAction {
  647. [self resumeRecord];
  648. }
  649. //手指移入事件
  650. - (void)didDragInsideAction {
  651. [self pauseRecord];
  652. }
  653. //获得声音 tableviewcell高度
  654. - (CGFloat)getSoundViewHeight:(NSMutableArray *)soundArray {
  655. // 上下间隔已经在frame上做了
  656. NSInteger row = soundArray.count;
  657. return row*50;
  658. }
  659. //添加消息
  660. - (void)addMessage:(DKMessage *)addedMessage {
  661. WEAKSELF
  662. [self exChangeMessageDataSourceQueue:^{
  663. NSMutableArray *messages = [NSMutableArray arrayWithArray:weakSelf.messages];
  664. [messages addObject:addedMessage];
  665. NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:1];
  666. [indexPaths addObject:[NSIndexPath indexPathForRow:messages.count - 1 inSection:0]];
  667. [weakSelf exMainQueue:^{
  668. weakSelf.messages = messages;
  669. [_messageTV insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
  670. [weakSelf scrollToBottomAnimated:YES];
  671. }];
  672. }];
  673. }
  674. //滚动到底部动画
  675. - (void)scrollToBottomAnimated:(BOOL)animated {
  676. NSInteger rows = [_messageTV numberOfRowsInSection:0];
  677. if (rows > 0) {
  678. [_messageTV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0]
  679. atScrollPosition:UITableViewScrollPositionBottom
  680. animated:animated];
  681. }
  682. }
  683. //滚动到指定行
  684. - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath
  685. atScrollPosition:(UITableViewScrollPosition)position
  686. animated:(BOOL)animated {
  687. [_messageTV scrollToRowAtIndexPath:indexPath
  688. atScrollPosition:position
  689. animated:animated];
  690. }
  691. //初始化布局
  692. -(void)initUI{
  693. scroll=[UIScrollView new];
  694. scroll.tag=1001;
  695. scroll.delegate=self;
  696. [self.view addSubview:scroll];
  697. scroll.frame=self.view.bounds;
  698. contentView=[[UIView alloc]init];
  699. contentView.frame=CGRectZero;
  700. self.view.backgroundColor=[UIColor whiteColor];
  701. [scroll addSubview:contentView];
  702. UITapGestureRecognizer* gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKey)];
  703. gesture.cancelsTouchesInView=NO;
  704. [contentView addGestureRecognizer:gesture];
  705. UILabel* lblUserName=[[UILabel alloc] init];
  706. lblUserName.frame=CGRectMake(15, 15, 100, 25);
  707. lblUserName.text=@"姓 名";
  708. lblUserName.font=logTextFont;
  709. [contentView addSubview:lblUserName];
  710. UITextField* textUserName=[[UITextField alloc] init];
  711. textUserName.frame=CGRectMake(130, 15, 150, 25);
  712. textUserName.textColor=ButtonTitleColor;
  713. textUserName.text=kkUserName;
  714. textUserName.font=logTextFont;
  715. textUserName.enabled=NO;
  716. [contentView addSubview:textUserName];
  717. UIView *userNameSeparator=[[UIView alloc]init];
  718. userNameSeparator.frame=CGRectMake(130,CGRectGetMaxY(textUserName.frame)+14,Screen_Width-145,1);
  719. userNameSeparator.backgroundColor=LineBackgroundColor;
  720. [contentView addSubview: userNameSeparator];
  721. UILabel* lblDate=[[UILabel alloc] init];
  722. lblDate.frame=CGRectMake(15,CGRectGetMaxY(userNameSeparator.frame)+15, 100, 25);
  723. lblDate.text=@"日 期";
  724. lblDate.font=logTextFont;
  725. [contentView addSubview:lblDate];
  726. _btnDate=[UIButton buttonWithType:UIButtonTypeCustom];
  727. _btnDate.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  728. _btnDate.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  729. _btnDate.frame=CGRectMake(130,CGRectGetMaxY(userNameSeparator.frame)+15, 150, 25);
  730. [_btnDate setTitle:@"请选择日志日期" forState:UIControlStateNormal];
  731. [_btnDate setTitleColor:ButtonTitleColor forState:UIControlStateNormal];
  732. _btnDate.titleLabel.font=logTextFont;
  733. [_btnDate addTarget:self action:@selector(showStartSelect) forControlEvents:UIControlEventTouchUpInside];
  734. [contentView addSubview:_btnDate];
  735. UIView *dateSeparator=[[UIView alloc]init];
  736. dateSeparator.frame=CGRectMake(130,CGRectGetMaxY(_btnDate.frame)+14,Screen_Width-145,1);
  737. dateSeparator.backgroundColor=LineBackgroundColor;
  738. [contentView addSubview: dateSeparator];
  739. UILabel* lblScope=[[UILabel alloc] init];
  740. lblScope.frame=CGRectMake(15,CGRectGetMaxY(dateSeparator.frame)+15, 100, 25);
  741. lblScope.text=@"范围权限";
  742. lblScope.font=logTextFont;
  743. [contentView addSubview:lblScope];
  744. _btnShare=[UIButton buttonWithType:UIButtonTypeCustom];
  745. _btnShare.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  746. _btnShare.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  747. _btnShare.frame=CGRectMake(130,CGRectGetMaxY(dateSeparator.frame)+15, 300, 25);
  748. [_btnShare setTitle:@"请选择范围权限" forState:UIControlStateNormal];
  749. [_btnShare setTitleColor:ButtonTitleColor forState:UIControlStateNormal];
  750. _btnShare.titleLabel.font=logTextFont;
  751. [_btnShare addTarget:self action:@selector(showScope) forControlEvents:UIControlEventTouchUpInside];
  752. [contentView addSubview:_btnShare];
  753. UIView *scopeSeparator=[[UIView alloc]init];
  754. scopeSeparator.frame=CGRectMake(130,CGRectGetMaxY(_btnShare.frame)+14,Screen_Width-145, 1);
  755. scopeSeparator.backgroundColor=LineBackgroundColor;
  756. [contentView addSubview: scopeSeparator];
  757. UILabel* lblChecker=[[UILabel alloc] init];
  758. lblChecker.frame=CGRectMake(15,CGRectGetMaxY(scopeSeparator.frame)+15, 100, 25);
  759. lblChecker.text=@"点 评 人";
  760. lblChecker.font=logTextFont;
  761. [contentView addSubview:lblChecker];
  762. _btnComment=[UIButton buttonWithType:UIButtonTypeCustom];
  763. _btnComment.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  764. _btnComment.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  765. _btnComment.frame=CGRectMake(130,CGRectGetMaxY(scopeSeparator.frame)+15, 150, 25);
  766. [_btnComment setTitle:@"请选择点评人" forState:UIControlStateNormal];
  767. [_btnComment setTitleColor:ButtonTitleColor forState:UIControlStateNormal];
  768. _btnComment.titleLabel.font=logTextFont;
  769. [_btnComment addTarget:self action:@selector(showComment) forControlEvents:UIControlEventTouchUpInside];
  770. [contentView addSubview:_btnComment];
  771. UIView *selectView=[[UIView alloc]init];
  772. selectView.frame=CGRectMake(0, CGRectGetMaxY(lblChecker.frame)+15, Screen_Width,40);
  773. selectView.backgroundColor=LineBackgroundColor;
  774. UILabel *selectBackgroundView=[[UILabel alloc]init];
  775. selectBackgroundView.frame=CGRectMake(14,13,100,25);
  776. UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"selectBackground"]];
  777. [selectBackgroundView setBackgroundColor:color];
  778. selectBackgroundView.text=@" 以下均为选填";
  779. selectBackgroundView.font=logTextFont;
  780. [selectView addSubview:selectBackgroundView];
  781. [contentView addSubview:selectView];
  782. _txtContent=[[UITextView alloc] init];
  783. _txtContent.frame=CGRectMake(15,CGRectGetMaxY(selectView.frame)+15,Screen_Width-30,100);
  784. _txtContent.textColor=[UIColor blackColor];
  785. _txtContent.font=logTextFont;
  786. _txtContent.delegate=self;
  787. _txtContent.tag=101;
  788. [contentView addSubview:_txtContent];
  789. UILabel *placeHolderLabel = [[UILabel alloc] init];
  790. placeHolderLabel.text = @"请填写日志内容";
  791. placeHolderLabel.numberOfLines = 0;
  792. placeHolderLabel.textColor = ButtonTitleColor;
  793. placeHolderLabel.font=logTextFont;
  794. [placeHolderLabel sizeToFit];
  795. [_txtContent addSubview: placeHolderLabel];
  796. [_txtContent setValue:placeHolderLabel forKey:@"_placeholderLabel"];
  797. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  798. flowLayout.itemSize = CGSizeMake(80,80);
  799. flowLayout.minimumInteritemSpacing = 5;
  800. flowLayout.minimumLineSpacing = 10;
  801. flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  802. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(_txtContent.frame)+8, Screen_Width-20, 1) collectionViewLayout:flowLayout];
  803. _collectionView.backgroundColor = [UIColor clearColor];
  804. _collectionView.dataSource = self;
  805. _collectionView.delegate = self;
  806. [_collectionView registerNib:[UINib nibWithNibName:@"LogNewCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"LogNewCollectionViewCell"];
  807. [contentView addSubview:_collectionView];
  808. _positionImg=[[UIImageView alloc]init];
  809. _positionImg.frame=CGRectMake(15, CGRectGetMaxY(_collectionView.frame),9,12);
  810. [_positionImg setImage:[UIImage imageNamed:@"log_position"]];
  811. [contentView addSubview:_positionImg];
  812. _lblPosition=[[UILabel alloc]init];
  813. _lblPosition.frame=CGRectMake(CGRectGetMaxX(_positionImg.frame)+11,CGRectGetMaxY(_collectionView.frame)+10, 350, 20);
  814. _lblPosition.font=logTextFont;
  815. [contentView addSubview:_lblPosition];
  816. _btnTakePhoto=[UIButton buttonWithType:UIButtonTypeCustom];
  817. _btnTakePhoto.frame=CGRectMake(15, CGRectGetMaxY(_positionImg.frame)+15,80,20);
  818. [contentView addSubview:_btnTakePhoto];
  819. UIImageView *takephotoImg=[[UIImageView alloc]init];
  820. takephotoImg.frame=CGRectMake(0,3, 18, 15);
  821. [takephotoImg setImage:[UIImage imageNamed:@"icon_takephoto"]];
  822. takephotoImg.userInteractionEnabled=NO;
  823. [_btnTakePhoto addSubview:takephotoImg];
  824. UILabel *lblTakePhoto=[[UILabel alloc]init];
  825. lblTakePhoto.text=@"照相";
  826. lblTakePhoto.font=logTextFont;
  827. lblTakePhoto.frame=CGRectMake(CGRectGetMaxX(takephotoImg.frame)+11,0,50, 20);
  828. [_btnTakePhoto addSubview:lblTakePhoto];
  829. [_btnTakePhoto addTarget:self action:@selector(selectCamera) forControlEvents:UIControlEventTouchUpInside];
  830. _btnPhoto=[UIButton buttonWithType:UIButtonTypeCustom];
  831. _btnPhoto.frame=CGRectMake((Screen_Width/3)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20);
  832. [contentView addSubview:_btnPhoto];
  833. UIImageView *photoImg=[[UIImageView alloc]init];
  834. photoImg.userInteractionEnabled=NO;
  835. photoImg.frame=CGRectMake(0,3, 18, 15);
  836. [photoImg setImage:[UIImage imageNamed:@"icon_photo"]];
  837. [_btnPhoto addSubview:photoImg];
  838. UILabel *lblPhoto=[[UILabel alloc]init];
  839. lblPhoto.text=@"相册";
  840. lblPhoto.font=logTextFont;
  841. lblPhoto.frame=CGRectMake(CGRectGetMaxX(photoImg.frame)+11,0,50, 20);
  842. [_btnPhoto addSubview:lblPhoto];
  843. [_btnPhoto addTarget:self action:@selector(selectPhotos) forControlEvents:UIControlEventTouchUpInside];
  844. _btnPosition=[UIButton buttonWithType:UIButtonTypeCustom];
  845. _btnPosition.frame=CGRectMake((Screen_Width/3*2)+15, CGRectGetMaxY(_positionImg.frame)+15,80,20);
  846. [contentView addSubview:_btnPosition];
  847. [_btnPosition addTarget:self action:@selector(selectPosition) forControlEvents:UIControlEventTouchUpInside];
  848. UIImageView *mapPositionImg=[[UIImageView alloc]init];
  849. mapPositionImg.userInteractionEnabled=NO;
  850. mapPositionImg.frame=CGRectMake(0,3, 13, 15);
  851. [ mapPositionImg setImage:[UIImage imageNamed:@"icon_position"]];
  852. [_btnPosition addSubview: mapPositionImg];
  853. UILabel *lblPosition=[[UILabel alloc]init];
  854. lblPosition.text=@"所在位置";
  855. lblPosition.font=logTextFont;
  856. lblPosition.frame=CGRectMake(CGRectGetMaxX(mapPositionImg.frame)+11,0,80, 20);
  857. [_btnPosition addSubview:lblPosition];
  858. _separatorView=[[UIView alloc]init];
  859. _separatorView.frame=CGRectMake(15, CGRectGetMaxY(_btnTakePhoto.frame)+15,Screen_Width-15, 1);
  860. _separatorView.backgroundColor=LineBackgroundColor;
  861. [contentView addSubview:_separatorView];
  862. _voiceImg=[[UIImageView alloc]init];
  863. _voiceImg.frame=CGRectMake(17,CGRectGetMaxY(_separatorView.frame)+18, 13, 15);
  864. [_voiceImg setImage:[UIImage imageNamed:@"icon_voice"]];
  865. [contentView addSubview:_voiceImg];
  866. _lblVoice=[[UILabel alloc]init];
  867. _lblVoice.text=@"语音";
  868. _lblVoice.font=logTextFont;
  869. _lblVoice.frame=CGRectMake(CGRectGetMaxX(_voiceImg.frame)+13,CGRectGetMaxY(_separatorView.frame)+15,30, 20);
  870. [contentView addSubview:_lblVoice];
  871. _messageTV = [[DKMessageTableView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_lblVoice.frame)+2, CGRectGetMaxY(_separatorView.frame)+18,190, 1) style:UITableViewStylePlain];
  872. _messageTV.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  873. _messageTV.dataSource = self;
  874. _messageTV.delegate = self;
  875. _messageTV.separatorStyle = UITableViewCellSeparatorStyleNone;
  876. [contentView addSubview:_messageTV];
  877. _btnRecord=[UIButton buttonWithType:UIButtonTypeCustom];
  878. _btnRecord.frame=CGRectMake(Screen_Width-90, CGRectGetMaxY(_separatorView.frame)+18,100,20);
  879. [contentView addSubview:_btnRecord];
  880. UIImageView *recordImg=[[UIImageView alloc]init];
  881. recordImg.frame=CGRectMake(0,3, 14, 15);
  882. recordImg.userInteractionEnabled=NO;
  883. [recordImg setImage:[UIImage imageNamed:@"longpressed"]];
  884. [_btnRecord addSubview:recordImg];
  885. [_btnRecord addTarget:self action:@selector(switchKeyboard) forControlEvents:UIControlEventTouchUpInside];
  886. UILabel *lblRecord=[[UILabel alloc]init];
  887. lblRecord.text=@"长按录音";
  888. lblRecord.textColor=LabelGrayTextColor;
  889. lblRecord.font=logTextFont;
  890. lblRecord.frame=CGRectMake(CGRectGetMaxX(recordImg.frame)+3,0,80, 20);
  891. [_btnRecord addSubview:lblRecord];
  892. contentView.frame=CGRectMake(0, 0, Screen_Width,CGRectGetMaxY(_btnRecord.frame));
  893. // 输入工具条的frame
  894. CGRect inputFrame = CGRectMake(0.0f,
  895. self.view.frame.size.height - 45.0,
  896. self.view.frame.size.width,
  897. 45.0);
  898. // 初始化输入工具条
  899. _inputView = [[DKMessageInputView alloc] initWithFrame:inputFrame];
  900. _inputView.allowsSendVoice = self.allowsSendVoice;
  901. _inputView.delegate = self;
  902. [self.view addSubview:_inputView];
  903. [self.view bringSubviewToFront:_inputView];
  904. }
  905. // 保存前的校验
  906. -(BOOL)check
  907. {
  908. if(_sDate ==nil || [NSString stringWithFormat:@"%@",_sDate ].length==0)
  909. {
  910. [self showAlertViewText:@"请选择日志日期"];
  911. return NO;
  912. }
  913. if(_txtContent.text ==nil || [NSString stringWithFormat:@"%@",_txtContent.text ].length==0)
  914. {
  915. [self showAlertViewText:@"请输入日志内容"];
  916. return NO;
  917. }
  918. if(_sShareId ==nil ||[NSString stringWithFormat:@"%@",_sShareId ].length==0)
  919. {
  920. [self showAlertViewText:@"请选择分享范围"];
  921. return NO;
  922. }
  923. if(_sCommentId ==nil || [NSString stringWithFormat:@"%@",_sCommentId ].length==0)
  924. {
  925. [self showAlertViewText:@"请选择点评人"];
  926. return NO;
  927. }
  928. return YES;
  929. }
  930. // 上传图片和语音
  931. -(BOOL)saveImageAndVioceManager{
  932. _mDownManager = [[ASIDownManager alloc] init];
  933. _mDownManager.delegate = self;
  934. //////////////上传语音
  935. NSString *urlStr = ServerURL;
  936. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  937. [dict setObject:@"SaveSound" forKey:@"Action"];
  938. [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"];
  939. [dict setObject:kkUserCode forKey:@"UserCode"];
  940. [dict setObject:kkUserPwd forKey:@"UserPassword"];
  941. [dict setObject:kkSessionKey forKey:@"SessionKey"];
  942. [self.voiceUploadPaths removeAllObjects];
  943. for (int i=0; i<self.voicePaths.count; i++) {
  944. NSString* retValue= [_mDownManager syncPostHttpRequestUrl:urlStr dic:dict path:self.voicePaths[i] filename:nil fileType: @"audio/x-flac"];
  945. // NSString* retValue= [_mDownManager SyncPostHttpRequest:urlStr :dict :self.voicePaths[i] :nil :@"audio/x-flac"];
  946. if (retValue==nil || retValue.length==0){
  947. [self cancel];
  948. [self showAlertViewText:@"上传语音失败"];
  949. self.navigationItem.rightBarButtonItem.enabled=YES;
  950. return NO;
  951. }
  952. NSDictionary *dicRetValue = [retValue JSONValue];
  953. if (dicRetValue && [dicRetValue isKindOfClass:[NSDictionary class]]) {
  954. int iStatus = [[dicRetValue objectForKey:@"Status"] intValue];
  955. //NSString *message=[dicRetValue objectForKey:@"Message"];
  956. if (iStatus == 0) {
  957. NSString * voicePath=[dicRetValue objectForKey:@"Result"];
  958. [self.voiceUploadPaths addObject:voicePath];
  959. }
  960. }
  961. }
  962. [self.imageLocalPaths removeAllObjects];
  963. [self.imagePaths removeAllObjects];
  964. _mDownManager = [[ASIDownManager alloc] init];
  965. _mDownManager.delegate = self;
  966. dict = [NSMutableDictionary dictionary];
  967. [dict setObject:@"SaveImg" forKey:@"Action"];
  968. [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"];
  969. [dict setObject:kkUserCode forKey:@"UserCode"];
  970. [dict setObject:kkUserPwd forKey:@"UserPassword"];
  971. [dict setObject:kkSessionKey forKey:@"SessionKey"];
  972. for (int i=0; i<self.assets.count; i++) {
  973. DKPhotoAssets *asset = self.assets[i];
  974. DKCamera * c;
  975. if ([asset isKindOfClass:[DKPhotoAssets class]]) {
  976. c=[self saveImage:asset.originImage withName:nil];
  977. }else if ([asset isKindOfClass:[NSString class]]){
  978. }else if([asset isKindOfClass:[UIImage class]]){
  979. }else if ([asset isKindOfClass:[DKCamera class]]){
  980. c=(DKCamera *)asset;
  981. }
  982. [self.imageLocalPaths addObject:c.imagePath];
  983. NSString* retValue= [_mDownManager syncPostHttpRequestUrl:urlStr dic:dict path:c.imagePath filename:nil fileType:@"image/jpeg"];
  984. //NSString* retValue= [_mDownManager SyncPostHttpRequest:urlStr :dict :c.imagePath :nil:@"image/jpeg"];
  985. if (retValue==nil || retValue.length==0){
  986. //[self Cancel];
  987. [self showAlertViewText:@"上传图片失败"];
  988. return NO;
  989. }
  990. NSDictionary *dicRetValue = [retValue JSONValue];
  991. if (dicRetValue && [dicRetValue isKindOfClass:[NSDictionary class]]) {
  992. int iStatus = [[dicRetValue objectForKey:@"Status"] intValue];
  993. //NSString *message=[dicRetValue objectForKey:@"Message"];
  994. if (iStatus == 0) {
  995. NSString * imgPath=[dicRetValue objectForKey:@"Result"];
  996. [self.imagePaths addObject:imgPath];
  997. }
  998. }
  999. }
  1000. return YES;
  1001. }
  1002. //画面清空
  1003. -(void)clearSenderData{
  1004. _txtContent.text=@"";
  1005. _sShareUserName=@"";
  1006. _sShareId=@"";
  1007. _sComment=@"";
  1008. _sCommentId=@"";
  1009. [_btnShare setTitle:@"请选择分享范围" forState:UIControlStateNormal];
  1010. [_btnComment setTitle:@"请选择点评人" forState:UIControlStateNormal];
  1011. self.lblPosition.text=@"";
  1012. vPositionView.hidden=YES;
  1013. UIDatePicker * picker=[[UIDatePicker alloc] init];
  1014. NSDate *select = [picker date];
  1015. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1016. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  1017. _sDate = [dateFormatter stringFromDate:select];
  1018. NSString *date= [DateFormat dateFormatSplit:_sDate];
  1019. [_btnDate setTitle:date forState:UIControlStateNormal];
  1020. NSFileManager * fileManager = [[NSFileManager alloc]init];
  1021. for (NSString* imgPath in self.imageLocalPaths ) {
  1022. [fileManager removeItemAtPath:imgPath error:nil];
  1023. }
  1024. for (NSString* voicePath in self.voiceUploadPaths) {
  1025. [fileManager removeItemAtPath:voicePath error:nil];
  1026. }
  1027. [self.imageLocalPaths removeAllObjects];
  1028. [self.voiceUploadPaths removeAllObjects];
  1029. [self.voiceUploadTimes removeAllObjects];
  1030. [self.voicePaths removeAllObjects];
  1031. [self.assets removeAllObjects];
  1032. [self.collectionView reloadData];
  1033. [self.messages removeAllObjects];
  1034. [_messageTV reloadData];
  1035. }
  1036. //取消进度条加载
  1037. - (void)cancel {
  1038. [self stopLoading];
  1039. }
  1040. //保存日志
  1041. -(void)saveLog{
  1042. if([self check]==NO){
  1043. return ;
  1044. }
  1045. self.navigationItem.rightBarButtonItem.enabled=NO;
  1046. [self startLoading];
  1047. // jiangyh add 2017-07-14 add
  1048. // 先保存图片 ,后保存数据
  1049. [self startLoading];
  1050. dispatch_async(dispatch_get_global_queue(0,0), ^{
  1051. if ([self saveImageAndVioceManager]) {
  1052. [self saveDataManager];
  1053. }
  1054. });
  1055. // if ([self saveImageAndVioceManager]) {
  1056. // [self saveDataManager];
  1057. // }
  1058. // jiangyh add 2017-07-14 end
  1059. }
  1060. //保存数据
  1061. -(void)saveDataManager{
  1062. _mDownManager = [[ASIDownManager alloc] init];
  1063. _mDownManager.delegate = self;
  1064. _mDownManager.OnImageDown = @selector(onLoadFinish:);
  1065. _mDownManager.OnImageFail = @selector(onLoadFail:);
  1066. NSString *urlStr = ServerURL;
  1067. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  1068. [dict setObject:@"SaveDailyRecordManager" forKey:@"Action"];
  1069. [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"];
  1070. [dict setObject:kkUserCode forKey:@"UserCode"];
  1071. [dict setObject:kkUserPwd forKey:@"UserPassword"];
  1072. [dict setObject:kkSessionKey forKey:@"SessionKey"];
  1073. [dict setObject:@"0" forKey:@"RecordStatus"];
  1074. [dict setObject:self.txtContent.text forKey:@"RecordContent"];
  1075. [dict setObject:@"1" forKey:@"RecordSource"];
  1076. [dict setObject:self.sDate forKey:@"CreateTime"];
  1077. [dict setObject:self.sShareId forKey:@"PreviewUserIDs"];
  1078. [dict setObject:self.sCommentId forKey:@"CommentUserID"];
  1079. if(self.sLocation!=nil && self.sLocation.length>0){
  1080. [dict setObject:self.sLocation forKey:@"RecordLocation"];
  1081. }
  1082. /////////图片上传返回的地址
  1083. if (self.imagePaths && self.imagePaths.count>0) {
  1084. [dict setObject:[self.imagePaths componentsJoinedByString:@"," ] forKey:@"ImagePath"];
  1085. }
  1086. if (self.voiceUploadPaths && self.voiceUploadPaths.count>0) {
  1087. [dict setObject:[self.voiceUploadPaths componentsJoinedByString:@"," ] forKey:@"SoundPath"];
  1088. }
  1089. if (self.voiceUploadTimes && self.voiceUploadTimes.count>0) {
  1090. [dict setObject:[self.voiceUploadTimes componentsJoinedByString:@"," ] forKey:@"SoundTime"];
  1091. }
  1092. [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];
  1093. }
  1094. //切换底部布局
  1095. -(void)switchKeyboard{
  1096. if(_inputView!=nil){
  1097. if(_inputView.saveButton.hidden==YES){
  1098. _inputView.saveButton.hidden=NO;
  1099. _inputView.switchButton.hidden=YES;
  1100. _inputView.voiceButton.hidden=YES;
  1101. }
  1102. else{
  1103. _inputView.saveButton.hidden=YES;
  1104. _inputView.switchButton.hidden=NO;
  1105. _inputView.voiceButton.hidden=NO;
  1106. }
  1107. }
  1108. }
  1109. //保存图片至沙盒
  1110. - (DKCamera *) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
  1111. {
  1112. if (!currentImage) {
  1113. return nil;
  1114. }
  1115. NSDateFormatter *formater = [[NSDateFormatter alloc] init];
  1116. formater.dateFormat = @"yyyyMMddHHmmss";
  1117. NSString *currentTimeStr = [[formater stringFromDate:[NSDate date]] stringByAppendingFormat:@"_%d_.jpg" ,arc4random_uniform(10000)];
  1118. NSData *imageData= [LeslieAsyncImageDownloader resetSizeOfImageData:currentImage maxSize:500];
  1119. // 获取沙盒目录
  1120. NSString *fullPath ;
  1121. if([imageName isEqualToString:@""] || imageName==nil){
  1122. fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:currentTimeStr];
  1123. }
  1124. else{
  1125. fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
  1126. }
  1127. // 将图片写入文件
  1128. [imageData writeToFile:fullPath atomically:NO];
  1129. UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
  1130. //NSData *data = UIImageJPEGRepresentation(savedImage, 0.3);
  1131. DKCamera *camera = [[DKCamera alloc] init];
  1132. camera.imagePath = fullPath;
  1133. camera.thumbImage =savedImage;// [UIImage imageWithData:data];
  1134. return camera;
  1135. }
  1136. //选择地图方法
  1137. -(void)selectPosition{
  1138. MapViewController *mc=[MapViewController new];
  1139. mc.pDelegate=self;
  1140. [self.navigationController pushViewController:mc animated:YES];
  1141. }
  1142. //相机拍照方法
  1143. -(void)selectCamera{
  1144. DKCameraViewController *cameraVc = [[DKCameraViewController alloc] init];
  1145. __weak typeof(self) weakSelf = self;
  1146. // 多选相册+相机多拍 回调
  1147. [cameraVc startCameraOrPhotoFileWithViewController:self complate:^(NSArray *object) {
  1148. // 选择完照片、拍照完回调
  1149. [object enumerateObjectsUsingBlock:^(id asset, NSUInteger idx, BOOL *stop) {
  1150. if(weakSelf.assets.count<9){
  1151. if ([asset isKindOfClass:[DKCamera class]]) {
  1152. [weakSelf.assets addObject:asset];
  1153. }else{
  1154. [weakSelf.assets addObject:asset];
  1155. }
  1156. }else{
  1157. [self showAlertViewText:@"最多只能选择9张图片"];
  1158. }
  1159. }];
  1160. [weakSelf.collectionView reloadData];
  1161. }];
  1162. self.cameraVc = cameraVc;
  1163. }
  1164. #pragma mark - select Photo Library
  1165. -(NSInteger)cameraCount{
  1166. NSInteger count=0;
  1167. for (int i=0; i<self.assets.count; i++) {
  1168. id asset=self.assets[i];
  1169. if ([asset isKindOfClass:[DKCamera class]]) {
  1170. count++;
  1171. }
  1172. }
  1173. return count;
  1174. }
  1175. //选择相册方法
  1176. - (void)selectPhotos {
  1177. // 创建控制器
  1178. DKPhotoPickerViewController *pickerVc = [[DKPhotoPickerViewController alloc] init];
  1179. // 默认显示相册里面的内容SavePhotos
  1180. pickerVc.status = PickerViewShowStatusCameraRoll;
  1181. pickerVc.selectPickers = self.assets;
  1182. // 最多能选9张图片
  1183. pickerVc.minCount = 9;
  1184. pickerVc.cameraCount=[self cameraCount];
  1185. pickerVc.delegate = self;
  1186. [pickerVc show];
  1187. }
  1188. // 获得相册collectionview的高度
  1189. - (CGFloat)getPhotoCollectionViewHeightWithPhotos:(NSMutableArray *)photos {
  1190. // 上下间隔已经在frame上做了
  1191. NSInteger row = (photos.count / 3 + (photos.count % 3 ? 1 : 0));
  1192. return (row *80 + (row *10));
  1193. }
  1194. //显示评论对话框
  1195. -(void)showComment{
  1196. if(_sShareId==nil || _sShareId.length==0){
  1197. [super showAlertViewText:@"请选择分享范围"];
  1198. return;
  1199. }
  1200. [self.view endEditing:YES];
  1201. self.hidesBottomBarWhenPushed=YES;
  1202. LogCommentVC *tc=[[LogCommentVC alloc] init];
  1203. tc.commentDelegate=self;
  1204. [tc.filterArr addObjectsFromArray: self.shareArr];
  1205. [self.navigationController pushViewController:tc animated:YES];
  1206. }
  1207. //显示分享范围对话框
  1208. -(void)showScope
  1209. {
  1210. [self.view endEditing:YES];
  1211. self.hidesBottomBarWhenPushed=YES;
  1212. LogShareVC *tc=[LogShareVC new];
  1213. tc.shareDelegate=self;
  1214. tc.tag=@"logAdd";
  1215. [self.navigationController pushViewController:tc animated:YES];
  1216. }
  1217. //显示开始日期控件
  1218. -(void)showStartSelect
  1219. {
  1220. __weak typeof(self) weakself=self;
  1221. _brdatePicker = [BRDatePickerView PickerAlertWithTitle:@""];
  1222. [_brdatePicker configureSelectionBlock:^(NSString *date){
  1223. _displayDate=date;
  1224. } andCompletionBlock:^(void){
  1225. UIDatePicker * picker=[[UIDatePicker alloc] init];
  1226. NSDate *select = [picker date];
  1227. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1228. [dateFormatter setDateFormat:@"HH:mm:ss"];
  1229. NSString *time = [dateFormatter stringFromDate:select];
  1230. _sDate = [NSString stringWithFormat: @"%@ %@",_displayDate,time];
  1231. [weakself.btnDate setTitle:_displayDate forState:UIControlStateNormal];
  1232. } andCancelBlock:^(void){
  1233. _displayDate=weakself.btnDate.titleLabel.text;
  1234. UIDatePicker * picker=[[UIDatePicker alloc] init];
  1235. NSDate *select = [picker date];
  1236. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1237. [dateFormatter setDateFormat:@"HH:mm:ss"];
  1238. NSString *time = [dateFormatter stringFromDate:select];
  1239. _sDate = [NSString stringWithFormat: @"%@ %@",_displayDate,time];
  1240. }];
  1241. [_brdatePicker show];
  1242. }
  1243. //隐藏键盘方法
  1244. -(void)hideKey
  1245. {
  1246. [self.view endEditing:YES];
  1247. }
  1248. /**
  1249. * 返回函数
  1250. */
  1251. -(void)goBack
  1252. {
  1253. [self.navigationController popViewControllerAnimated:YES];
  1254. if([self.refreshDelegate respondsToSelector:@selector(refreshData)]){
  1255. [self.refreshDelegate refreshData];
  1256. }
  1257. }
  1258. @end