HXPhotoView.m 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. //
  2. // HXPhotoView.m
  3. // HXPhotoPickerExample
  4. //
  5. // Created by Silence on 17/2/17.
  6. // Copyright © 2017年 Silence. All rights reserved.
  7. //
  8. #import "HXPhotoView.h"
  9. #import "UIView+HXExtension.h"
  10. #import <MobileCoreServices/MobileCoreServices.h>
  11. #import <MediaPlayer/MediaPlayer.h>
  12. #import "UIImage+HXExtension.h"
  13. #import "HXAlbumListViewController.h"
  14. #import "HXPhotoPreviewViewController.h"
  15. #import "HXCustomNavigationController.h"
  16. #import "HXCustomCameraViewController.h"
  17. #import "HXPhotoViewController.h"
  18. #import "HXPhotoBottomSelectView.h"
  19. #import "UIColor+HXExtension.h"
  20. @interface HXPhotoView ()<HXCollectionViewDataSource,HXCollectionViewDelegate, UICollectionViewDelegateFlowLayout,HXPhotoSubViewCellDelegate,UIAlertViewDelegate,HXAlbumListViewControllerDelegate,HXCustomCameraViewControllerDelegate,HXPhotoPreviewViewControllerDelegate, HXPhotoViewControllerDelegate, HXCustomNavigationControllerDelegate>
  21. @property (strong, nonatomic) NSMutableArray *dataList;
  22. @property (strong, nonatomic) NSMutableArray *photos;
  23. @property (strong, nonatomic) NSMutableArray *videos;
  24. @property (strong, nonatomic) HXPhotoModel *addModel;
  25. @property (assign, nonatomic) BOOL isAddModel;
  26. @property (assign, nonatomic) BOOL original;
  27. @property (strong, nonatomic) UICollectionViewFlowLayout *flowLayout;
  28. @property (assign, nonatomic) NSInteger numOfLinesOld;
  29. @property (assign, nonatomic) BOOL downLoadComplete;
  30. @property (strong, nonatomic) UIImage *tempCameraImage;
  31. @property (strong, nonatomic) UIImagePickerController* imagePickerController;
  32. @property (strong, nonatomic) HXPhotoSubViewCell *addCell;
  33. @property (assign, nonatomic) BOOL tempShowAddCell;
  34. @property (assign, nonatomic) CGFloat lastWidth;
  35. @end
  36. @implementation HXPhotoView
  37. @synthesize addImageName = _addImageName;
  38. @synthesize addDarkImageName = _addDarkImageName;
  39. @synthesize manager = _manager;
  40. - (UICollectionViewFlowLayout *)flowLayout {
  41. if (!_flowLayout) {
  42. _flowLayout = [[UICollectionViewFlowLayout alloc] init];
  43. _flowLayout.scrollDirection = self.scrollDirection;
  44. }
  45. return _flowLayout;
  46. }
  47. - (NSMutableArray *)dataList {
  48. if (!_dataList) {
  49. _dataList = [NSMutableArray array];
  50. }
  51. return _dataList;
  52. }
  53. - (HXPhotoModel *)addModel {
  54. if (!_addModel) {
  55. _addModel = [[HXPhotoModel alloc] init];
  56. _addModel.type = HXPhotoModelMediaTypeCamera;
  57. _addModel.thumbPhoto = [UIImage hx_imageNamed:self.addImageName];
  58. _addModel.previewPhoto = [UIImage hx_imageNamed:self.addDarkImageName];
  59. }
  60. return _addModel;
  61. }
  62. - (HXPhotoManager *)manager {
  63. if (!_manager) {
  64. _manager = [[HXPhotoManager alloc] init];
  65. }
  66. return _manager;
  67. }
  68. + (instancetype)photoManager:(HXPhotoManager *)manager {
  69. return [[self alloc] initWithManager:manager];
  70. }
  71. + (instancetype)photoManager:(HXPhotoManager *)manager scrollDirection:(UICollectionViewScrollDirection)scrollDirection {
  72. return [[self alloc] initWithManager:manager scrollDirection:scrollDirection];
  73. }
  74. - (instancetype)initWithCoder:(NSCoder *)coder {
  75. if (self = [super initWithCoder:coder]) {
  76. [self setup];
  77. }
  78. return self;
  79. }
  80. - (instancetype)initWithFrame:(CGRect)frame manager:(HXPhotoManager *)manager {
  81. return [self initWithFrame:frame manager:manager scrollDirection:UICollectionViewScrollDirectionVertical];
  82. }
  83. - (instancetype)initWithFrame:(CGRect)frame manager:(HXPhotoManager *)manager scrollDirection:(UICollectionViewScrollDirection)scrollDirection {
  84. self = [super initWithFrame:frame];
  85. if (self) {
  86. self.scrollDirection = scrollDirection;
  87. _manager = manager;
  88. [self setup];
  89. }
  90. return self;
  91. }
  92. - (instancetype)initWithManager:(HXPhotoManager *)manager {
  93. return [self initWithManager:manager scrollDirection:UICollectionViewScrollDirectionVertical];
  94. }
  95. - (instancetype)initWithManager:(HXPhotoManager *)manager scrollDirection:(UICollectionViewScrollDirection)scrollDirection {
  96. self = [super init];
  97. if (self) {
  98. self.scrollDirection = scrollDirection;
  99. _manager = manager;
  100. [self setup];
  101. }
  102. return self;
  103. }
  104. - (instancetype)initWithFrame:(CGRect)frame {
  105. self = [super initWithFrame:frame];
  106. if (self) {
  107. [self setup];
  108. }
  109. return self;
  110. }
  111. - (void)awakeFromNib {
  112. [super awakeFromNib];
  113. [self setup];
  114. }
  115. - (HXCollectionView *)collectionView {
  116. if (!_collectionView) {
  117. _collectionView = [[HXCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
  118. if (self.scrollDirection != UICollectionViewScrollDirectionHorizontal) {
  119. _collectionView.scrollEnabled = NO;
  120. }
  121. _collectionView.dataSource = self;
  122. _collectionView.delegate = self;
  123. _collectionView.backgroundColor = self.backgroundColor;
  124. [_collectionView registerClass:[HXPhotoSubViewCell class] forCellWithReuseIdentifier:@"HXPhotoSubViewCellId"];
  125. [_collectionView registerClass:[HXPhotoSubViewCell class] forCellWithReuseIdentifier:@"addCell"];
  126. }
  127. return _collectionView;
  128. }
  129. - (NSInteger)lineCount {
  130. if (!_lineCount) {
  131. _lineCount = 3;
  132. }
  133. return _lineCount;
  134. }
  135. - (void)setScrollDirection:(UICollectionViewScrollDirection)scrollDirection {
  136. _scrollDirection = scrollDirection;
  137. if (scrollDirection == UICollectionViewScrollDirectionHorizontal) {
  138. self.collectionView.scrollEnabled = YES;
  139. }else {
  140. self.collectionView.scrollEnabled = NO;
  141. }
  142. self.flowLayout.scrollDirection = scrollDirection;
  143. }
  144. - (void)setBackgroundColor:(UIColor *)backgroundColor {
  145. self.collectionView.backgroundColor = backgroundColor;
  146. [super setBackgroundColor:backgroundColor];
  147. }
  148. - (void)setup {
  149. self.maximumHeight = HX_ScreenHeight;
  150. self.lastWidth = 0;
  151. if (_manager) {
  152. _manager.configuration.specialModeNeedHideVideoSelectBtn = YES;
  153. }
  154. self.spacing = 3;
  155. self.lineCount = 3;
  156. self.numOfLinesOld = 0;
  157. self.tag = 9999;
  158. _showAddCell = YES;
  159. self.tempShowAddCell = YES;
  160. self.previewShowBottomPageControl = YES;
  161. self.adaptiveDarkness = YES;
  162. self.flowLayout.minimumLineSpacing = self.spacing;
  163. self.flowLayout.minimumInteritemSpacing = self.spacing;
  164. [self addSubview:self.collectionView];
  165. if (_manager.afterSelectedArray.count > 0) {
  166. if ([self.delegate respondsToSelector:@selector(photoListViewControllerDidDone:allList:photos:videos:original:)]) {
  167. [self.delegate photoListViewControllerDidDone:self allList:self.manager.afterSelectedArray.copy photos:self.manager.afterSelectedPhotoArray.copy videos:self.manager.afterSelectedVideoArray.copy original:self.manager.afterOriginal];
  168. }
  169. [self setupDataWithAllList:self.manager.afterSelectedArray.copy photos:self.manager.afterSelectedPhotoArray.copy videos:self.manager.afterSelectedVideoArray.copy original:self.manager.afterOriginal];
  170. }
  171. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshView) name:@"HXPhotoViewNeedReloadNotification" object:nil];
  172. }
  173. - (void)jumpPreviewViewControllerWithModel:(HXPhotoModel *)model {
  174. if (![self.manager.afterSelectedArray containsObject:model]) {
  175. NSSLog(@"model有误!!!");
  176. return;
  177. }
  178. HXPhotoPreviewViewController *vc = [[HXPhotoPreviewViewController alloc] init];
  179. vc.disableaPersentInteractiveTransition = self.disableaInteractiveTransition;
  180. vc.outside = YES;
  181. vc.manager = self.manager;
  182. vc.exteriorPreviewStyle = self.previewStyle;
  183. vc.delegate = self;
  184. vc.modelArray = [NSMutableArray arrayWithArray:self.manager.afterSelectedArray];
  185. vc.currentModelIndex = [self.manager.afterSelectedArray indexOfObject:model];
  186. vc.previewShowDeleteButton = self.previewShowDeleteButton;
  187. vc.showBottomPageControl = self.previewShowBottomPageControl;
  188. vc.photoView = self;
  189. vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
  190. vc.modalPresentationCapturesStatusBarAppearance = YES;
  191. [[self hx_viewController] presentViewController:vc animated:YES completion:nil];
  192. }
  193. - (void)jumpPreviewViewControllerWithIndex:(NSInteger)index {
  194. if (index >= 0 && index < self.manager.afterSelectedArray.count) {
  195. HXPhotoModel *model = self.manager.afterSelectedArray[index];
  196. [self jumpPreviewViewControllerWithModel:model];
  197. }else {
  198. NSSLog(@"越界啦!!!");
  199. }
  200. }
  201. - (HXPhotoSubViewCell *)previewingContextViewWithPoint:(CGPoint)point {
  202. CGPoint cPoint = [self convertPoint:point toView:self.collectionView];
  203. NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:cPoint];
  204. if (indexPath) {
  205. HXPhotoSubViewCell *cell = (HXPhotoSubViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
  206. if (cell.model.type == HXPhotoModelMediaTypeCamera) {
  207. return nil;
  208. }
  209. return cell;
  210. }
  211. return nil;
  212. }
  213. - (HXPhotoSubViewCell *)collectionViewCellWithIndex:(NSInteger)index {
  214. if (index < 0 || index > self.dataList.count - 1 || !self.dataList.count) {
  215. return nil;
  216. }
  217. HXPhotoSubViewCell *cell = (HXPhotoSubViewCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
  218. if (cell.model.type == HXPhotoModelMediaTypeCamera) {
  219. return nil;
  220. }
  221. return cell;
  222. }
  223. - (void)setEditEnabled:(BOOL)editEnabled {
  224. _editEnabled = editEnabled;
  225. self.collectionView.editEnabled = editEnabled;
  226. }
  227. - (void)setManager:(HXPhotoManager *)manager {
  228. _manager = manager;
  229. manager.configuration.specialModeNeedHideVideoSelectBtn = YES;
  230. if (self.manager.afterSelectedArray.count > 0) {
  231. if ([self.delegate respondsToSelector:@selector(photoListViewControllerDidDone:allList:photos:videos:original:)]) {
  232. [self.delegate photoListViewControllerDidDone:self allList:self.manager.afterSelectedArray.copy photos:self.manager.afterSelectedPhotoArray.copy videos:self.manager.afterSelectedVideoArray.copy original:self.manager.afterOriginal];
  233. }
  234. [self setupDataWithAllList:self.manager.afterSelectedArray.copy photos:self.manager.afterSelectedPhotoArray.copy videos:self.manager.afterSelectedVideoArray.copy original:self.manager.afterOriginal];
  235. }
  236. }
  237. - (void)setHideDeleteButton:(BOOL)hideDeleteButton {
  238. _hideDeleteButton = hideDeleteButton;
  239. [self.collectionView reloadData];
  240. }
  241. - (void)setShowAddCell:(BOOL)showAddCell {
  242. _showAddCell = showAddCell;
  243. self.tempShowAddCell = showAddCell;
  244. if (self.manager.afterSelectedArray.count > 0) {
  245. [self setupDataWithAllList:self.manager.afterSelectedArray.copy photos:self.manager.afterSelectedPhotoArray.copy videos:self.manager.afterSelectedVideoArray.copy original:self.manager.afterOriginal];
  246. }
  247. }
  248. - (NSString *)addImageName {
  249. if (!_addImageName) {
  250. _addImageName = @"hx_list_add_img";
  251. }
  252. return _addImageName;
  253. }
  254. - (NSString *)addDarkImageName {
  255. if (!_addDarkImageName) {
  256. _addDarkImageName = @"hx_list_add_dark_img";
  257. }
  258. return _addDarkImageName;
  259. }
  260. - (void)setAddImageName:(NSString *)addImageName {
  261. _addImageName = addImageName;
  262. self.addModel.thumbPhoto = [UIImage hx_imageNamed:addImageName];
  263. if (self.tempShowAddCell) {
  264. [self.collectionView reloadData];
  265. }
  266. }
  267. - (void)setAddDarkImageName:(NSString *)addDarkImageName {
  268. _addDarkImageName = addDarkImageName;
  269. self.addModel.previewPhoto = [UIImage hx_imageNamed:addDarkImageName];
  270. if (self.tempShowAddCell) {
  271. [self.collectionView reloadData];
  272. }
  273. }
  274. - (void)setDeleteImageName:(NSString *)deleteImageName {
  275. _deleteImageName = deleteImageName;
  276. [self.collectionView reloadData];
  277. }
  278. /**
  279. 刷新视图
  280. */
  281. - (void)refreshView {
  282. if (!self.manager.configuration.singleSelected) {
  283. [self setupDataWithAllList:self.manager.afterSelectedArray.copy photos:self.manager.afterSelectedPhotoArray.copy videos:self.manager.afterSelectedVideoArray.copy original:self.manager.afterOriginal];
  284. }
  285. }
  286. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  287. return self.tempShowAddCell ? self.dataList.count + 1 : self.dataList.count;
  288. }
  289. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  290. return 1;
  291. }
  292. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  293. if (self.tempShowAddCell) {
  294. if (indexPath.item == self.dataList.count) {
  295. HXPhotoSubViewCell *addCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"addCell" forIndexPath:indexPath];
  296. addCell.index = indexPath.item;
  297. addCell.model = self.addModel;
  298. return addCell;
  299. }
  300. }
  301. HXPhotoSubViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HXPhotoSubViewCellId" forIndexPath:indexPath];
  302. cell.index = indexPath.item;
  303. cell.customProtocol = self.cellCustomProtocol;
  304. cell.delegate = self;
  305. cell.canEdit = self.collectionView.editEnabled;
  306. if (self.deleteImageName) {
  307. cell.deleteImageName = self.deleteImageName;
  308. }
  309. cell.model = self.dataList[indexPath.item];
  310. cell.showDeleteNetworkPhotoAlert = self.showDeleteNetworkPhotoAlert;
  311. cell.deleteCellShowAlert = self.deleteCellShowAlert;
  312. cell.hideDeleteButton = self.hideDeleteButton;
  313. return cell;
  314. }
  315. - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  316. if (self.tempShowAddCell && indexPath.item == self.dataList.count) {
  317. return YES;
  318. }
  319. BOOL canSelect = YES;
  320. if ([self.delegate respondsToSelector:@selector(photoView:collectionViewShouldSelectItemAtIndexPath:model:)]) {
  321. HXPhotoModel *model = self.dataList[indexPath.item];
  322. canSelect = [self.delegate photoView:self collectionViewShouldSelectItemAtIndexPath:indexPath model:model];
  323. }
  324. return canSelect;
  325. }
  326. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  327. if (self.tempShowAddCell) {
  328. if (indexPath.item == self.dataList.count) {
  329. if ([self.delegate respondsToSelector:@selector(photoViewDidAddCellClick:)]) {
  330. [self.delegate photoViewDidAddCellClick:self];
  331. }
  332. if (self.didAddCellBlock) {
  333. self.didAddCellBlock(self);
  334. }
  335. if (self.interceptAddCellClick) {
  336. return;
  337. }
  338. [self goPhotoViewController];
  339. return;
  340. }
  341. }
  342. self.currentIndexPath = indexPath;
  343. HXPhotoModel *model = self.dataList[indexPath.item];
  344. if (model.networkPhotoUrl) {
  345. if (model.downloadError) {
  346. HXPhotoSubViewCell *cell = (HXPhotoSubViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
  347. [cell againDownload];
  348. return;
  349. }
  350. }
  351. if (model.type == HXPhotoModelMediaTypeCamera) {
  352. if ([self.delegate respondsToSelector:@selector(photoViewDidAddCellClick:)]) {
  353. [self.delegate photoViewDidAddCellClick:self];
  354. }
  355. if (self.didAddCellBlock) {
  356. self.didAddCellBlock(self);
  357. }
  358. if (self.interceptAddCellClick) {
  359. return;
  360. }
  361. [self goPhotoViewController];
  362. }else {
  363. HXPhotoPreviewViewController *vc = [[HXPhotoPreviewViewController alloc] init];
  364. vc.disableaPersentInteractiveTransition = self.disableaInteractiveTransition;
  365. vc.outside = YES;
  366. vc.manager = self.manager;
  367. vc.exteriorPreviewStyle = self.previewStyle;
  368. vc.delegate = self;
  369. vc.modelArray = [NSMutableArray arrayWithArray:self.dataList];
  370. vc.currentModelIndex = [self.dataList indexOfObject:model];
  371. vc.previewShowDeleteButton = self.previewShowDeleteButton;
  372. vc.showBottomPageControl = self.previewShowBottomPageControl;
  373. vc.photoView = self;
  374. vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
  375. vc.modalPresentationCapturesStatusBarAppearance = YES;
  376. [[self hx_viewController] presentViewController:vc animated:YES completion:nil];
  377. }
  378. }
  379. #if HXPhotoViewCustomItemSize || HXPhotoViewCustomItemSizeEnable
  380. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  381. BOOL isAddItem = NO;
  382. if (self.tempShowAddCell && indexPath.item == self.dataList.count) {
  383. isAddItem = YES;
  384. }
  385. CGSize size = CGSizeMake(100, 100);
  386. if ([self.delegate respondsToSelector:@selector(collectionView:layout:sizeForItemAtIndexPath:isAddItem:photoView:)]) {
  387. size = [self.delegate collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:indexPath isAddItem:isAddItem photoView:self];
  388. }
  389. return size;
  390. }
  391. #endif
  392. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  393. if ([self.delegate respondsToSelector:@selector(collectionView:layout:minimumLineSpacingForSectionAtIndex:photoView:)]) {
  394. return [self.delegate collectionView:collectionView layout:collectionViewLayout minimumLineSpacingForSectionAtIndex:section photoView:self];
  395. }
  396. return self.spacing;
  397. }
  398. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  399. if ([self.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:photoView:)]) {
  400. return [self.delegate collectionView:collectionView layout:collectionViewLayout minimumInteritemSpacingForSectionAtIndex:section photoView:self];
  401. }
  402. return self.spacing;
  403. }
  404. #pragma mark - < HXPhotoPreviewViewControllerDelegate >
  405. - (void)photoPreviewControllerDidCancel:(HXPhotoPreviewViewController *)previewController model:(HXPhotoModel *)model {
  406. if ([self.delegate respondsToSelector:@selector(photoViewPreviewDismiss:)]) {
  407. [self.delegate photoViewPreviewDismiss:self];
  408. }
  409. }
  410. - (void)photoPreviewCellDownloadImageComplete:(HXPhotoPreviewViewController *)previewController model:(HXPhotoModel *)model {
  411. if (!model.loadOriginalImage) {
  412. NSIndexPath *indexPath = [self currentModelIndexPath:model];
  413. HXPhotoSubViewCell *cell = (HXPhotoSubViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
  414. [cell resetNetworkImage];
  415. }
  416. }
  417. - (void)photoPreviewDidDeleteClick:(HXPhotoPreviewViewController *)previewController deleteModel:(HXPhotoModel *)model deleteIndex:(NSInteger)index {
  418. [self deleteModelWithIndex:index];
  419. }
  420. - (void)photoPreviewSelectLaterDidEditClick:(HXPhotoPreviewViewController *)previewController beforeModel:(HXPhotoModel *)beforeModel afterModel:(HXPhotoModel *)afterModel {
  421. if (self.manager.configuration.useWxPhotoEdit && afterModel.subType == HXPhotoModelMediaSubTypePhoto) {
  422. [self.collectionView reloadData];
  423. [self dragCellCollectionViewCellEndMoving:self.collectionView];
  424. return;
  425. }
  426. [self.manager afterSelectedArrayReplaceModelAtModel:beforeModel withModel:afterModel];
  427. [self.manager afterSelectedListAddEditPhotoModel:afterModel];
  428. [self.photos removeAllObjects];
  429. [self.videos removeAllObjects];
  430. NSInteger i = 0;
  431. for (HXPhotoModel *model in self.manager.afterSelectedArray) {
  432. model.selectIndexStr = [NSString stringWithFormat:@"%ld",i + 1];
  433. if (model.subType == HXPhotoModelMediaSubTypePhoto) {
  434. [self.photos addObject:model];
  435. }else if (model.subType == HXPhotoModelMediaSubTypeVideo) {
  436. [self.videos addObject:model];
  437. }
  438. i++;
  439. }
  440. [self.manager setAfterSelectedPhotoArray:self.photos];
  441. [self.manager setAfterSelectedVideoArray:self.videos];
  442. [self.dataList replaceObjectAtIndex:[self.dataList indexOfObject:beforeModel] withObject:afterModel];
  443. [self.collectionView reloadData];
  444. [self dragCellCollectionViewCellEndMoving:self.collectionView];
  445. }
  446. /**
  447. 添加按钮点击事件
  448. */
  449. - (void)goPhotoViewController {
  450. if (self.outerCamera) {
  451. HXPhotoBottomViewModel *shootingModel = [[HXPhotoBottomViewModel alloc] init];
  452. shootingModel.subTitleDarkColor = [UIColor hx_colorWithHexStr:@"#999999"];
  453. if (self.manager.type == HXPhotoManagerSelectedTypePhoto) {
  454. if (self.manager.configuration.photoMaxNum > 0) {
  455. self.manager.configuration.maxNum = self.manager.configuration.photoMaxNum;
  456. }
  457. shootingModel.title = [NSBundle hx_localizedStringForKey:@"拍照"];
  458. }else if (self.manager.type == HXPhotoManagerSelectedTypeVideo) {
  459. if (self.manager.configuration.videoMaxNum > 0) {
  460. self.manager.configuration.maxNum = self.manager.configuration.videoMaxNum;
  461. }
  462. shootingModel.title = [NSBundle hx_localizedStringForKey:@"摄像"];
  463. }else {
  464. shootingModel.title = [NSBundle hx_localizedStringForKey:@"拍摄"];
  465. if (self.manager.configuration.photoMaxNum > 0 &&
  466. self.manager.configuration.videoMaxNum > 0) {
  467. self.manager.configuration.maxNum = self.manager.configuration.videoMaxNum + self.manager.configuration.photoMaxNum;
  468. }
  469. if (!self.manager.configuration.selectTogether) {
  470. if (self.manager.afterSelectedPhotoArray.count) {
  471. shootingModel.title = [NSBundle hx_localizedStringForKey:@"拍照"];
  472. }else if (self.manager.afterSelectedVideoArray.count) {
  473. shootingModel.title = [NSBundle hx_localizedStringForKey:@"摄像"];
  474. }else {
  475. shootingModel.subTitle = [NSBundle hx_localizedStringForKey:@"照片或视频"];
  476. shootingModel.cellHeight = 65.f;
  477. }
  478. }else {
  479. shootingModel.subTitle = [NSBundle hx_localizedStringForKey:@"照片或视频"];
  480. shootingModel.cellHeight = 65.f;
  481. }
  482. }
  483. HXPhotoBottomViewModel *selectModel = [[HXPhotoBottomViewModel alloc] init];
  484. selectModel.title = [NSBundle hx_localizedStringForKey:@"从手机相册选择"];
  485. HXWeakSelf
  486. HXPhotoBottomSelectView *selectView = [HXPhotoBottomSelectView showSelectViewWithModels:@[shootingModel, selectModel] headerView:nil showTopLineView:NO cancelTitle:nil selectCompletion:^(NSInteger index, HXPhotoBottomViewModel * _Nonnull model) {
  487. if (index == 0) {
  488. [weakSelf goCameraViewController];
  489. }else if (index == 1) {
  490. [weakSelf directGoPhotoViewController];
  491. }
  492. } cancelClick:nil];
  493. selectView.adaptiveDarkness = self.adaptiveDarkness;
  494. return;
  495. }
  496. [self directGoPhotoViewController];
  497. }
  498. - (void)directGoPhotoViewController {
  499. HXCustomNavigationController *nav = [[HXCustomNavigationController alloc] initWithManager:self.manager delegate:self];
  500. nav.modalPresentationStyle = UIModalPresentationOverFullScreen;
  501. nav.modalPresentationCapturesStatusBarAppearance = YES;
  502. [[self hx_viewController] presentViewController:nav animated:YES completion:nil];
  503. }
  504. #pragma mark - < HXCustomNavigationControllerDelegate >
  505. - (void)photoNavigationViewController:(HXCustomNavigationController *)photoNavigationViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original {
  506. if ([self.delegate respondsToSelector:@selector(photoListViewControllerDidDone:allList:photos:videos:original:)]) {
  507. [self.delegate photoListViewControllerDidDone:self allList:allList photos:photoList videos:videoList original:original];
  508. }
  509. if ([self.delegate respondsToSelector:@selector(photoViewCurrentSelected:photos:videos:original:)]) {
  510. NSMutableArray *allModel = [NSMutableArray array];
  511. NSMutableArray *photoModels = [NSMutableArray array];
  512. NSMutableArray *videoModels = [NSMutableArray array];
  513. NSMutableArray *tempAll = allList.mutableCopy;
  514. for (HXPhotoModel *pModel in self.dataList) {
  515. for (HXPhotoModel *subPModel in tempAll) {
  516. if ([pModel isEqualToPhotoModel:subPModel]) {
  517. [tempAll removeObject:subPModel];
  518. break;
  519. }
  520. }
  521. }
  522. for (HXPhotoModel *photoModel in tempAll) {
  523. if (photoModel.subType == HXPhotoModelMediaSubTypePhoto) {
  524. [photoModels addObject:photoModel];
  525. }else if (photoModel.subType == HXPhotoModelMediaSubTypeVideo) {
  526. [videoModels addObject:photoModel];
  527. }
  528. [allModel addObject:photoModel];
  529. }
  530. [self.delegate photoViewCurrentSelected:allModel.copy photos:photoModels.copy videos:videoModels.copy original:original];
  531. }
  532. [self setupDataWithAllList:allList photos:photoList videos:videoList original:original];
  533. }
  534. - (void)photoNavigationViewControllerDidCancel:(HXCustomNavigationController *)photoNavigationViewController {
  535. if ([self.delegate respondsToSelector:@selector(photoViewDidCancel:)]) {
  536. [self.delegate photoViewDidCancel:self];
  537. }
  538. if (self.didCancelBlock) {
  539. self.didCancelBlock();
  540. }
  541. }
  542. /**
  543. 前往相机
  544. */
  545. - (void)goCameraViewController {
  546. if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  547. [[self hx_viewController].view hx_showImageHUDText:[NSBundle hx_localizedStringForKey:@"无法使用相机!"]];
  548. return;
  549. }
  550. HXWeakSelf
  551. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  552. dispatch_async(dispatch_get_main_queue(), ^{
  553. if (granted) {
  554. if (weakSelf.manager.configuration.replaceCameraViewController) {
  555. HXPhotoConfigurationCameraType cameraType;
  556. if (weakSelf.manager.type == HXPhotoManagerSelectedTypePhoto) {
  557. cameraType = HXPhotoConfigurationCameraTypePhoto;
  558. }else if (weakSelf.manager.type == HXPhotoManagerSelectedTypeVideo) {
  559. cameraType = HXPhotoConfigurationCameraTypeVideo;
  560. }else {
  561. if (!weakSelf.manager.configuration.selectTogether) {
  562. if (weakSelf.manager.afterSelectedPhotoArray.count > 0) {
  563. cameraType = HXPhotoConfigurationCameraTypePhoto;
  564. }else if (weakSelf.manager.afterSelectedVideoArray.count > 0) {
  565. cameraType = HXPhotoConfigurationCameraTypeVideo;
  566. }else {
  567. cameraType = HXPhotoConfigurationCameraTypePhotoAndVideo;
  568. }
  569. }else {
  570. cameraType = HXPhotoConfigurationCameraTypePhotoAndVideo;
  571. }
  572. }
  573. switch (weakSelf.manager.configuration.customCameraType) {
  574. case HXPhotoCustomCameraTypePhoto:
  575. cameraType = HXPhotoConfigurationCameraTypePhoto;
  576. break;
  577. case HXPhotoCustomCameraTypeVideo:
  578. cameraType = HXPhotoConfigurationCameraTypeVideo;
  579. break;
  580. case HXPhotoCustomCameraTypePhotoAndVideo:
  581. cameraType = HXPhotoConfigurationCameraTypePhotoAndVideo;
  582. break;
  583. default:
  584. break;
  585. }
  586. if (weakSelf.manager.configuration.shouldUseCamera) {
  587. weakSelf.manager.configuration.shouldUseCamera([weakSelf hx_viewController], cameraType, weakSelf.manager);
  588. }
  589. weakSelf.manager.configuration.useCameraComplete = ^(HXPhotoModel *model) {
  590. [weakSelf customCameraViewController:nil didDone:model];
  591. };
  592. return;
  593. }
  594. HXCustomCameraViewController *vc = [[HXCustomCameraViewController alloc] init];
  595. vc.delegate = weakSelf;
  596. vc.manager = weakSelf.manager;
  597. vc.isOutside = YES;
  598. HXCustomNavigationController *nav = [[HXCustomNavigationController alloc] initWithRootViewController:vc];
  599. nav.isCamera = YES;
  600. nav.supportRotation = weakSelf.manager.configuration.supportRotation;
  601. nav.modalPresentationStyle = UIModalPresentationOverFullScreen;
  602. nav.modalPresentationCapturesStatusBarAppearance = YES;
  603. [[weakSelf hx_viewController] presentViewController:nav animated:YES completion:nil];
  604. }else {
  605. [HXPhotoTools showUnusableCameraAlert:weakSelf.hx_viewController];
  606. }
  607. });
  608. }];
  609. }
  610. - (void)customCameraViewController:(HXCustomCameraViewController *)viewController didDone:(HXPhotoModel *)model {
  611. [self cameraDidNextClick:model];
  612. }
  613. /**
  614. 相机拍完之后的代理
  615. @param model 照片模型
  616. */
  617. - (void)cameraDidNextClick:(HXPhotoModel *)model {
  618. // 判断类型
  619. if (self.manager.shouldSelectModel) {
  620. NSString *str = self.manager.shouldSelectModel(model);
  621. if (str) {
  622. [[self hx_viewController].view hx_showImageHUDText:str];
  623. return;
  624. }
  625. }
  626. if (model.subType == HXPhotoModelMediaSubTypePhoto) {
  627. if (self.manager.type == HXPhotoManagerSelectedTypeVideo) {
  628. [[self hx_viewController].view hx_showImageHUDText:[NSBundle hx_localizedStringForKey:@"图片不能和视频同时选择"]];
  629. return;
  630. }
  631. // 当选择图片个数没有达到最大个数时就添加到选中数组中
  632. if ([self.manager afterSelectPhotoCountIsMaximum]) {
  633. NSInteger maxCount = self.manager.configuration.photoMaxNum > 0 ? self.manager.configuration.photoMaxNum : self.manager.configuration.maxNum;
  634. [[self hx_viewController].view hx_showImageHUDText:[NSString stringWithFormat:[NSBundle hx_localizedStringForKey:@"最多只能选择%ld张图片"],maxCount]];
  635. return;
  636. }
  637. }else if (model.subType == HXPhotoModelMediaSubTypeVideo) {
  638. if (self.manager.type == HXPhotoManagerSelectedTypePhoto) {
  639. [[self hx_viewController].view hx_showImageHUDText:[NSBundle hx_localizedStringForKey:@"视频不能和图片同时选择"]];
  640. return;
  641. }
  642. // 当选中视频个数没有达到最大个数时就添加到选中数组中
  643. if (round(model.videoDuration) < self.manager.configuration.videoMinimumSelectDuration) {
  644. [[self hx_viewController].view hx_showImageHUDText:[NSString stringWithFormat:[NSBundle hx_localizedStringForKey:@"视频少于%ld秒,无法选择"], self.manager.configuration.videoMinimumSelectDuration]];
  645. return;
  646. }else if (round(model.videoDuration) >= self.manager.configuration.videoMaximumSelectDuration + 1) {
  647. [[self hx_viewController].view hx_showImageHUDText:[NSString stringWithFormat:[NSBundle hx_localizedStringForKey:@"视频大于%ld秒,无法选择"], self.manager.configuration.videoMaximumSelectDuration]];
  648. return;
  649. }else if ([self.manager afterSelectVideoCountIsMaximum]) {
  650. NSInteger maxCount = self.manager.configuration.videoMaxNum > 0 ? self.manager.configuration.videoMaxNum : self.manager.configuration.maxNum;
  651. [[self hx_viewController].view hx_showImageHUDText:[NSString stringWithFormat:[NSBundle hx_localizedStringForKey:@"最多只能选择%ld个视频"],maxCount]];
  652. return;
  653. }
  654. }
  655. [self.manager afterListAddCameraTakePicturesModel:model];
  656. if ([self.delegate respondsToSelector:@selector(photoViewCameraTakePictureCompletion:model:)]) {
  657. [self.delegate photoViewCameraTakePictureCompletion:self model:model];
  658. }
  659. if ([self.delegate respondsToSelector:@selector(photoListViewControllerDidDone:allList:photos:videos:original:)]) {
  660. [self.delegate photoListViewControllerDidDone:self allList:self.manager.afterSelectedArray.copy photos:self.manager.afterSelectedPhotoArray.copy videos:self.manager.afterSelectedVideoArray.copy original:self.manager.afterOriginal];
  661. }
  662. [self setupDataWithAllList:self.manager.afterSelectedArray.copy photos:self.manager.afterSelectedPhotoArray.copy videos:self.manager.afterSelectedVideoArray.copy original:self.manager.afterOriginal];
  663. }
  664. - (void)deleteModelWithIndex:(NSInteger)index {
  665. if (index < 0) {
  666. index = 0;
  667. }
  668. if (index > self.manager.afterSelectedArray.count - 1) {
  669. index = self.manager.afterSelectedArray.count - 1;
  670. }
  671. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
  672. if (cell) {
  673. [self cellDidDeleteClcik:cell];
  674. }else {
  675. if (HXShowLog) NSSLog(@"删除失败 - cell为空");
  676. }
  677. }
  678. /**
  679. cell删除按钮的代理
  680. @param cell 被删的cell
  681. */
  682. - (void)cellDidDeleteClcik:(UICollectionViewCell *)cell {
  683. if (!cell) {
  684. return;
  685. }
  686. [(HXPhotoSubViewCell *)cell imageView].image = nil;
  687. NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
  688. HXPhotoModel *model = self.dataList[indexPath.item];
  689. model.photoEdit = nil;
  690. [self.manager afterSelectedListdeletePhotoModel:model];
  691. if ((model.type == HXPhotoModelMediaTypePhoto || model.type == HXPhotoModelMediaTypePhotoGif) || (model.type == HXPhotoModelMediaTypeCameraPhoto || model.type == HXPhotoModelMediaTypeLivePhoto)) {
  692. [self.photos removeObject:model];
  693. }else if (model.type == HXPhotoModelMediaTypeVideo || model.type == HXPhotoModelMediaTypeCameraVideo) {
  694. [self.videos removeObject:model];
  695. }
  696. UIView *mirrorView = [cell snapshotViewAfterScreenUpdates:NO];
  697. mirrorView.frame = cell.frame;
  698. [self.collectionView insertSubview:mirrorView atIndex:0];
  699. cell.hidden = YES;
  700. [UIView animateWithDuration:0.25 animations:^{
  701. mirrorView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
  702. } completion:^(BOOL finished) {
  703. cell.hidden = NO;
  704. HXPhotoSubViewCell *myCell = (HXPhotoSubViewCell *)cell;
  705. myCell.imageView.image = nil;
  706. [mirrorView removeFromSuperview];
  707. }];
  708. [self.dataList removeObjectAtIndex:indexPath.item];
  709. [self.collectionView performBatchUpdates:^{
  710. [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
  711. } completion:^(BOOL finished) {
  712. BOOL collectionReload = YES;
  713. if (self.showAddCell) {
  714. if (!self.tempShowAddCell) {
  715. self.tempShowAddCell = YES;
  716. [CATransaction begin];
  717. [CATransaction setDisableActions:YES];
  718. [self.collectionView reloadData];
  719. dispatch_async(dispatch_get_main_queue(), ^{
  720. [CATransaction commit];
  721. });
  722. collectionReload = NO;
  723. }
  724. }
  725. if (self.cellCustomProtocol && collectionReload) {
  726. [CATransaction begin];
  727. [CATransaction setDisableActions:YES];
  728. [self.collectionView reloadData];
  729. dispatch_async(dispatch_get_main_queue(), ^{
  730. [CATransaction commit];
  731. });
  732. }
  733. [self setupNewFrame];
  734. }];
  735. [self changeSelectedListModelIndex];
  736. if (model.networkPhotoUrl) {
  737. if ([self.delegate respondsToSelector:@selector(photoView:deleteNetworkPhoto:)]) {
  738. [self.delegate photoView:self deleteNetworkPhoto:model.networkPhotoUrl.absoluteString];
  739. }
  740. if (self.deleteNetworkPhotoBlock) {
  741. self.deleteNetworkPhotoBlock(model.networkPhotoUrl.absoluteString);
  742. }
  743. }
  744. if ([self.delegate respondsToSelector:@selector(photoView:changeComplete:photos:videos:original:)]) {
  745. [self.delegate photoView:self changeComplete:self.dataList.copy photos:self.photos.copy videos:self.videos.copy original:self.original];
  746. }
  747. if ([self.delegate respondsToSelector:@selector(photoListViewControllerDidDone:allList:photos:videos:original:)]) {
  748. [self.delegate photoListViewControllerDidDone:self allList:self.manager.afterSelectedArray.copy photos:self.manager.afterSelectedPhotoArray.copy videos:self.manager.afterSelectedVideoArray.copy original:self.manager.afterOriginal];
  749. }
  750. if ([self.delegate respondsToSelector:@selector(photoViewChangeComplete:allAssetList:photoAssets:videoAssets:original:)]) {
  751. NSMutableArray *allAsset = [NSMutableArray array];
  752. NSMutableArray *photoAssets = [NSMutableArray array];
  753. NSMutableArray *videoAssets = [NSMutableArray array];
  754. for (HXPhotoModel *phMd in self.dataList) {
  755. if (phMd.asset) {
  756. if (phMd.subType == HXPhotoModelMediaSubTypePhoto) {
  757. [photoAssets addObject:phMd.asset];
  758. }else {
  759. [videoAssets addObject:phMd.asset];
  760. }
  761. [allAsset addObject:phMd.asset];
  762. }
  763. }
  764. [self.delegate photoViewChangeComplete:self allAssetList:allAsset photoAssets:photoAssets videoAssets:videoAssets original:self.original];
  765. }
  766. if (self.changeCompleteBlock) {
  767. self.changeCompleteBlock(self.dataList.copy, self.photos.copy, self.videos.copy, self.original);
  768. }
  769. if ([self.delegate respondsToSelector:@selector(photoView:currentDeleteModel:currentIndex:)]) {
  770. [self.delegate photoView:self currentDeleteModel:model currentIndex:indexPath.item];
  771. }
  772. if (self.currentDeleteModelBlock) {
  773. self.currentDeleteModelBlock(model, indexPath.item);
  774. }
  775. if (model.type != HXPhotoModelMediaTypeCameraPhoto &&
  776. model.type != HXPhotoModelMediaTypeCameraVideo) {
  777. model.thumbPhoto = nil;
  778. model.previewPhoto = nil;
  779. model = nil;
  780. }
  781. }
  782. - (void)changeSelectedListModelIndex {
  783. int i = 0;
  784. for (HXPhotoModel *model in self.dataList) {
  785. model.selectedIndex = i;
  786. model.selectIndexStr = [NSString stringWithFormat:@"%d",i + 1];
  787. i++;
  788. }
  789. }
  790. - (void)setupDataWithAllList:(NSArray *)allList photos:(NSArray *)photos videos:(NSArray *)videos original:(BOOL)original {
  791. self.original = original;
  792. NSMutableArray *tempAllArray = [NSMutableArray array];
  793. NSMutableArray *tempPhotoArray = [NSMutableArray array];
  794. [tempAllArray addObjectsFromArray:allList];
  795. [tempPhotoArray addObjectsFromArray:photos];
  796. allList = tempAllArray;
  797. photos = tempPhotoArray;
  798. self.photos = [NSMutableArray arrayWithArray:photos];
  799. self.videos = [NSMutableArray arrayWithArray:videos];
  800. [self.dataList removeAllObjects];
  801. [self.dataList addObjectsFromArray:allList];
  802. if (self.showAddCell) {
  803. self.tempShowAddCell = YES;
  804. if (self.manager.configuration.selectTogether) {
  805. if (self.manager.configuration.maxNum == allList.count) {
  806. self.tempShowAddCell = NO;
  807. }
  808. }else {
  809. if (photos.count > 0) {
  810. NSInteger maxCount = self.manager.configuration.photoMaxNum > 0 ? self.manager.configuration.photoMaxNum : self.manager.configuration.maxNum;
  811. if (photos.count == maxCount) {
  812. if (maxCount > 0) {
  813. self.tempShowAddCell = NO;
  814. }
  815. }
  816. }else if (videos.count > 0) {
  817. NSInteger maxCount = self.manager.configuration.videoMaxNum > 0 ? self.manager.configuration.videoMaxNum : self.manager.configuration.maxNum;
  818. if (videos.count == maxCount) {
  819. if (maxCount > 0) {
  820. self.tempShowAddCell = NO;
  821. }
  822. }
  823. }
  824. }
  825. }
  826. [self changeSelectedListModelIndex];
  827. [self.collectionView reloadData];
  828. if ([self.delegate respondsToSelector:@selector(photoView:changeComplete:photos:videos:original:)]) {
  829. [self.delegate photoView:self changeComplete:allList.copy photos:photos.copy videos:videos.copy original:original];
  830. }
  831. if ([self.delegate respondsToSelector:@selector(photoViewChangeComplete:allAssetList:photoAssets:videoAssets:original:)]) {
  832. NSMutableArray *allAsset = [NSMutableArray array];
  833. NSMutableArray *photoAssets = [NSMutableArray array];
  834. NSMutableArray *videoAssets = [NSMutableArray array];
  835. for (HXPhotoModel *phMd in allList) {
  836. if (phMd.asset) {
  837. if (phMd.subType == HXPhotoModelMediaSubTypePhoto) {
  838. [photoAssets addObject:phMd.asset];
  839. }else {
  840. [videoAssets addObject:phMd.asset];
  841. }
  842. [allAsset addObject:phMd.asset];
  843. }
  844. }
  845. [self.delegate photoViewChangeComplete:self allAssetList:allAsset photoAssets:photoAssets videoAssets:videoAssets original:original];
  846. }
  847. if (self.changeCompleteBlock) {
  848. self.changeCompleteBlock(allList.copy, photos.copy, videos.copy, original);
  849. }
  850. [self layoutSubviews];
  851. if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal &&
  852. self.dataList.count) {
  853. NSInteger currentItem = self.tempShowAddCell ? self.dataList.count : self.dataList.count - 1;
  854. [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:currentItem inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
  855. }
  856. }
  857. - (NSArray *)dataSourceArrayOfCollectionView:(HXCollectionView *)collectionView {
  858. return self.dataList;
  859. }
  860. - (void)dragCellCollectionView:(HXCollectionView *)collectionView newDataArrayAfterMove:(NSArray *)newDataArray {
  861. self.dataList = [NSMutableArray arrayWithArray:newDataArray];
  862. }
  863. - (void)dragCellCollectionView:(HXCollectionView *)collectionView moveCellFromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
  864. HXPhotoModel *fromModel = self.dataList[fromIndexPath.item];
  865. HXPhotoModel *toModel = self.dataList[toIndexPath.item];
  866. [self.manager afterSelectedArraySwapPlacesWithFromModel:fromModel fromIndex:fromIndexPath.item toModel:toModel toIndex:toIndexPath.item];
  867. [self.photos removeAllObjects];
  868. [self.videos removeAllObjects];
  869. NSInteger i = 0;
  870. for (HXPhotoModel *model in self.manager.afterSelectedArray) {
  871. model.selectIndexStr = [NSString stringWithFormat:@"%ld",i + 1];
  872. if (model.subType == HXPhotoModelMediaSubTypePhoto) {
  873. [self.photos addObject:model];
  874. }else if (model.subType == HXPhotoModelMediaSubTypeVideo) {
  875. [self.videos addObject:model];
  876. }
  877. i++;
  878. }
  879. [self.manager setAfterSelectedPhotoArray:self.photos];
  880. [self.manager setAfterSelectedVideoArray:self.videos];
  881. }
  882. - (void)dragCellCollectionViewCellEndMoving:(HXCollectionView *)collectionView {
  883. if ([self.delegate respondsToSelector:@selector(photoView:changeComplete:photos:videos:original:)]) {
  884. [self.delegate photoView:self changeComplete:self.dataList.copy photos:self.photos.copy videos:self.videos.copy original:self.original];
  885. }
  886. if ([self.delegate respondsToSelector:@selector(photoListViewControllerDidDone:allList:photos:videos:original:)]) {
  887. [self.delegate photoListViewControllerDidDone:self allList:self.manager.afterSelectedArray.copy photos:self.manager.afterSelectedPhotoArray.copy videos:self.manager.afterSelectedVideoArray.copy original:self.manager.afterOriginal];
  888. }
  889. if ([self.delegate respondsToSelector:@selector(photoViewChangeComplete:allAssetList:photoAssets:videoAssets:original:)]) {
  890. NSMutableArray *allAsset = [NSMutableArray array];
  891. NSMutableArray *photoAssets = [NSMutableArray array];
  892. NSMutableArray *videoAssets = [NSMutableArray array];
  893. for (HXPhotoModel *phMd in self.dataList) {
  894. if (phMd.asset) {
  895. if (phMd.subType == HXPhotoModelMediaSubTypePhoto) {
  896. [photoAssets addObject:phMd.asset];
  897. }else {
  898. [videoAssets addObject:phMd.asset];
  899. }
  900. [allAsset addObject:phMd.asset];
  901. }
  902. }
  903. [self.delegate photoViewChangeComplete:self allAssetList:allAsset photoAssets:photoAssets videoAssets:videoAssets original:self.original];
  904. }
  905. if (self.changeCompleteBlock) {
  906. self.changeCompleteBlock(self.dataList.copy, self.photos.copy, self.videos.copy, self.original);
  907. }
  908. }
  909. - (void)collectionViewNeedReloadData:(UICollectionView *)collectionView {
  910. if (self.cellCustomProtocol) {
  911. [self.collectionView reloadData];
  912. }
  913. }
  914. - (BOOL)collectionViewShouldDeleteCurrentMoveItem:(UICollectionView *)collectionView gestureRecognizer:(UILongPressGestureRecognizer *)longPgr indexPath:(NSIndexPath *)indexPath {
  915. if ([self.delegate respondsToSelector:@selector(photoViewShouldDeleteCurrentMoveItem:gestureRecognizer:indexPath:)]) {
  916. return [self.delegate photoViewShouldDeleteCurrentMoveItem:self gestureRecognizer:longPgr indexPath:indexPath];
  917. }
  918. if (self.shouldDeleteCurrentMoveItemBlock) {
  919. return self.shouldDeleteCurrentMoveItemBlock(longPgr, indexPath);
  920. }
  921. return NO;
  922. }
  923. - (void)collectionView:(UICollectionView *)collectionView gestureRecognizerBegan:(UILongPressGestureRecognizer *)longPgr indexPath:(NSIndexPath *)indexPath {
  924. if (indexPath) {
  925. if ([self.delegate respondsToSelector:@selector(photoView:gestureRecognizerBegan:indexPath:)]) {
  926. [self.delegate photoView:self gestureRecognizerBegan:longPgr indexPath:indexPath];
  927. }
  928. if (self.longGestureRecognizerBeganBlock) {
  929. self.longGestureRecognizerBeganBlock(longPgr, indexPath);
  930. }
  931. }
  932. }
  933. - (void)collectionView:(UICollectionView *)collectionView gestureRecognizerChange:(UILongPressGestureRecognizer *)longPgr indexPath:(NSIndexPath *)indexPath {
  934. if (indexPath) {
  935. HXPhotoSubViewCell *cell = (HXPhotoSubViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
  936. if (cell.model.type == HXPhotoModelMediaTypeCamera) {
  937. return;
  938. }
  939. if ([self.delegate respondsToSelector:@selector(photoView:gestureRecognizerChange:indexPath:)]) {
  940. [self.delegate photoView:self gestureRecognizerChange:longPgr indexPath:indexPath];
  941. }
  942. if (self.longGestureRecognizerChangeBlock) {
  943. self.longGestureRecognizerChangeBlock(longPgr, indexPath);
  944. }
  945. }
  946. }
  947. - (void)collectionView:(UICollectionView *)collectionView gestureRecognizerEnded:(UILongPressGestureRecognizer *)longPgr indexPath:(NSIndexPath *)indexPath {
  948. if (indexPath) {
  949. if ([self.delegate respondsToSelector:@selector(photoView:gestureRecognizerEnded:indexPath:)]) {
  950. [self.delegate photoView:self gestureRecognizerEnded:longPgr indexPath:indexPath];
  951. }
  952. if (self.longGestureRecognizerEndedBlock) {
  953. self.longGestureRecognizerEndedBlock(longPgr, indexPath);
  954. }
  955. }
  956. }
  957. - (NSIndexPath *)currentModelIndexPath:(HXPhotoModel *)model {
  958. if ([self.dataList containsObject:model]) {
  959. return [NSIndexPath indexPathForItem:[self.dataList indexOfObject:model] inSection:0];
  960. }
  961. return [NSIndexPath indexPathForItem:0 inSection:0];
  962. }
  963. #pragma mark - < 更新高度 >
  964. - (void)setupNewFrame {
  965. BOOL needUpdateFrame = NO;
  966. if (self.lastWidth) {
  967. if (self.lastWidth != self.hx_w) {
  968. needUpdateFrame = YES;
  969. }
  970. }
  971. UIEdgeInsets insets = self.collectionView.contentInset;
  972. CGFloat itemW = (NSInteger)((self.hx_w - self.spacing * (self.lineCount - 1) - insets.left - insets.right) / self.lineCount);
  973. if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal &&
  974. itemW > 20) {
  975. itemW -= 10;
  976. }
  977. if (itemW <= 0) {
  978. itemW = 100;
  979. }
  980. #if HXPhotoViewCustomItemSizeEnable
  981. itemW = 100;
  982. #else
  983. if (!HXPhotoViewCustomItemSize) {
  984. self.flowLayout.itemSize = CGSizeMake(itemW, itemW);
  985. }else {
  986. itemW = 100;
  987. }
  988. #endif
  989. NSInteger dataCount = self.tempShowAddCell ? self.dataList.count + 1 : self.dataList.count;
  990. NSInteger numOfLinesNew = 0;
  991. if (self.lineCount != 0) numOfLinesNew = (dataCount / self.lineCount) + 1;
  992. if (dataCount % self.lineCount == 0) numOfLinesNew -= 1;
  993. self.flowLayout.minimumLineSpacing = self.spacing;
  994. self.flowLayout.minimumInteritemSpacing = self.spacing;
  995. if (numOfLinesNew != self.numOfLinesOld || needUpdateFrame) {
  996. self.numOfLinesOld = numOfLinesNew;
  997. CGFloat newHeight;
  998. if ([self.delegate respondsToSelector:@selector(photoViewHeight:)] ||
  999. [self.delegate respondsToSelector:@selector(collectionView:layout:sizeForItemAtIndexPath:isAddItem:photoView:)]) {
  1000. if (![self.delegate respondsToSelector:@selector(photoViewHeight:)]) {
  1001. NSAssert(NO, @"请实现此代理 - (CGFloat)photoViewHeight:(HXPhotoView *)photoView");
  1002. }
  1003. newHeight = [self.delegate photoViewHeight:self];
  1004. if (newHeight <= 0) {
  1005. newHeight = 0;
  1006. self.numOfLinesOld = 0;
  1007. }
  1008. self.hx_h = newHeight;
  1009. }else {
  1010. newHeight = numOfLinesNew * itemW + self.spacing * (numOfLinesNew - 1);
  1011. if (newHeight <= 0) {
  1012. newHeight = 0;
  1013. self.numOfLinesOld = 0;
  1014. }
  1015. if (newHeight > self.maximumHeight) {
  1016. newHeight = self.maximumHeight;
  1017. _collectionView.scrollEnabled = YES;
  1018. }else {
  1019. if (self.scrollDirection != UICollectionViewScrollDirectionHorizontal) {
  1020. _collectionView.scrollEnabled = NO;
  1021. }
  1022. }
  1023. if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
  1024. self.hx_h = itemW;
  1025. }else {
  1026. self.hx_h = newHeight;
  1027. }
  1028. }
  1029. if ([self.delegate respondsToSelector:@selector(photoView:updateFrame:)]) {
  1030. [self.delegate photoView:self updateFrame:self.frame];
  1031. }
  1032. BOOL hasConstraints = self.constraints.count;
  1033. if (hasConstraints) {
  1034. self.collectionView.frame = self.bounds;
  1035. }
  1036. if (self.updateFrameBlock) {
  1037. self.updateFrameBlock(self.frame);
  1038. }
  1039. }
  1040. self.lastWidth = self.hx_w;
  1041. }
  1042. - (void)layoutSubviews {
  1043. [super layoutSubviews];
  1044. if (self.hx_w <= 0) {
  1045. return;
  1046. }
  1047. if (self.lineCount <= 0) self.lineCount = 1;
  1048. NSInteger dataCount = self.tempShowAddCell ? self.dataList.count + 1 : self.dataList.count;
  1049. [self setupNewFrame];
  1050. CGFloat width = self.frame.size.width;
  1051. CGFloat height = self.frame.size.height;
  1052. BOOL hasConstraints = self.constraints.count;
  1053. if (dataCount == 1) {
  1054. if ([self.delegate respondsToSelector:@selector(photoViewHeight:)] ||
  1055. [self.delegate respondsToSelector:@selector(collectionView:layout:sizeForItemAtIndexPath:isAddItem:photoView:)]) {
  1056. if (![self.delegate respondsToSelector:@selector(photoViewHeight:)]) {
  1057. NSAssert(NO, @"请实现此代理 - (CGFloat)photoViewHeight:(HXPhotoView *)photoView");
  1058. }
  1059. if (self.constraints.count) {
  1060. NSAssert(NO, @"自定义HXPhotoView高度时,请不要使用约束布局");
  1061. }
  1062. CGFloat height = [self.delegate photoViewHeight:self];
  1063. if (height != self.hx_h) {
  1064. self.hx_h = height;
  1065. }
  1066. }else {
  1067. UIEdgeInsets insets = self.collectionView.contentInset;
  1068. CGFloat itemW = (width - self.spacing * (self.lineCount - 1) - insets.left - insets.right) / self.lineCount;
  1069. CGFloat roundH = roundf(height);
  1070. CGFloat roundW = roundf(itemW);
  1071. if (roundH != roundW && fabs(height - itemW) >= 2) {
  1072. if (itemW != self.hx_h && !hasConstraints) {
  1073. self.hx_h = itemW;
  1074. }
  1075. }
  1076. }
  1077. }
  1078. if (!hasConstraints) {
  1079. self.collectionView.frame = self.bounds;
  1080. if (self.collectionView.hx_h <= 0) {
  1081. self.numOfLinesOld = 0;
  1082. [self setupNewFrame];
  1083. self.collectionView.frame = self.bounds;
  1084. }
  1085. }
  1086. }
  1087. - (void)dealloc {
  1088. if (HXShowLog) NSSLog(@"dealloc");
  1089. }
  1090. @end