ImageSelectorViewController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // ImageSelectorViewController.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 17/1/19.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "ImageSelectorViewController.h"
  9. #import "ImageSelectorCollectionCell.h"
  10. static NSString* const imageSelectCellID = @"imageSelectCellID";
  11. @interface ImageSelectorViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
  12. CGRect _initFrame;
  13. }
  14. @property (nonatomic, strong) JXCollectionView * collectionView;
  15. @property (nonatomic, assign) NSInteger selectIndex;
  16. @end
  17. @implementation ImageSelectorViewController
  18. - (instancetype)init{
  19. self = [super init];
  20. if (self) {
  21. [UIApplication sharedApplication].statusBarHidden = NO;
  22. self.heightHeader = JX_SCREEN_TOP;
  23. self.heightFooter = JX_SCREEN_BOTTOM;
  24. self.isGotoBack = YES;
  25. self.isFreeOnClose = YES;
  26. //self.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  27. [self createHeadAndFoot];
  28. self.tableBody.backgroundColor = THEMEBACKCOLOR;
  29. self.tableBody.scrollEnabled = YES;
  30. _selectIndex = 0;
  31. [self customView];
  32. // [g_notify addObserver:self selector:@selector(changeSelectCell:) name:@"ImageSelectirCollectionCellSetSelected" object:nil];
  33. }
  34. return self;
  35. }
  36. - (void)dealloc{
  37. NSLog(@"%@.dealloc",NSStringFromClass([self class]));
  38. // [g_notify removeObserver:self name:@"ImageSelectirCollectionCellSetSelected" object:nil];
  39. // [super dealloc];
  40. }
  41. -(void)actionQuit{
  42. self.imageFileNameArray = nil;
  43. [super actionQuit];
  44. }
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. // Do any additional setup after loading the view.
  48. }
  49. - (void)didReceiveMemoryWarning {
  50. [super didReceiveMemoryWarning];
  51. // Dispose of any resources that can be recreated.
  52. }
  53. -(void)customView{
  54. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  55. _collectionView = [[JXCollectionView alloc] initWithFrame:CGRectMake(0, 0, self.tableBody.frame.size.width, self.tableBody.frame.size.height) collectionViewLayout:layout];
  56. // [layout release];
  57. _collectionView.allowsSelection = YES;
  58. _collectionView.backgroundColor = THEMEBACKCOLOR;
  59. _collectionView.contentSize = CGSizeMake(0, self.tableBody.frame.size.height+10);
  60. _collectionView.delegate = self;
  61. _collectionView.dataSource = self;
  62. _collectionView.userInteractionEnabled = YES;
  63. [_collectionView registerClass:[ImageSelectorCollectionCell class] forCellWithReuseIdentifier:imageSelectCellID];
  64. [self.tableBody addSubview:_collectionView];
  65. // [_collectionView release];
  66. UIButton * submitButton = [UIButton buttonWithType:UIButtonTypeSystem];
  67. submitButton.frame = CGRectMake((JX_SCREEN_WIDTH-130)/2, JX_SCREEN_HEIGHT+(JX_SCREEN_BOTTOM-38)/2-JX_SCREEN_BOTTOM, 130, 38);
  68. [submitButton setTitle:Localized(@"JX_Confirm") forState:UIControlStateNormal];
  69. [submitButton setTitle:Localized(@"JX_Confirm") forState:UIControlStateHighlighted];
  70. submitButton.titleLabel.font = [UIFont systemFontOfSize:17];
  71. [submitButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
  72. [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  73. [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
  74. [submitButton setBackgroundColor:THEMECOLOR];
  75. submitButton.layer.cornerRadius = 3;
  76. [submitButton addTarget:self action:@selector(submitImage) forControlEvents:UIControlEventTouchUpInside];
  77. [self.view addSubview:submitButton];
  78. }
  79. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  80. return _imageFileNameArray.count;
  81. }
  82. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  83. {
  84. return CGSizeMake((JX_SCREEN_WIDTH - 30)/2, (JX_SCREEN_WIDTH - 30)/2);
  85. }
  86. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  87. return UIEdgeInsetsMake(10, 10, 0, 10);
  88. }
  89. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  90. return 10.0;
  91. }
  92. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  93. return 10.0;
  94. }
  95. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  96. ImageSelectorCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:imageSelectCellID forIndexPath:indexPath];
  97. cell.didImageView = @selector(didImageView:);
  98. cell.didSelectView = @selector(didSelectView:);
  99. cell.delegate = self;
  100. cell.index = indexPath.row;
  101. cell.isSelected = [self cellIsSelectedIndex:indexPath.item];
  102. [cell refreshCellWithImagePath:_imageFileNameArray[indexPath.item]];
  103. return cell;
  104. }
  105. -(BOOL)cellIsSelectedIndex:(long)index{
  106. return index == _selectIndex;
  107. }
  108. -(void) showFullScreenViewImage:(UIImage*)image{
  109. UIImageView *zoomView = [[UIImageView alloc] initWithFrame:_initFrame];
  110. zoomView.image = image;
  111. zoomView.backgroundColor = [UIColor blackColor];
  112. zoomView.contentMode = UIViewContentModeScaleAspectFit;
  113. zoomView.userInteractionEnabled = YES;
  114. [self.view addSubview:zoomView];
  115. // [zoomView release];
  116. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenFullScreenImage:)];
  117. [zoomView addGestureRecognizer:tap];
  118. // [tap release];
  119. [UIView animateWithDuration:0.3 animations:^{
  120. zoomView.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  121. }];
  122. }
  123. -(void)hiddenFullScreenImage:(UITapGestureRecognizer *)tapGesture{
  124. [UIView animateWithDuration:0.3 animations:^{
  125. tapGesture.view.frame = _initFrame;
  126. } completion:^(BOOL finished) {
  127. [tapGesture.view removeFromSuperview];
  128. }];
  129. }
  130. -(void)submitImage{
  131. if (_selectIndex) {
  132. //
  133. }
  134. if (_imgDelegete && [_imgDelegete respondsToSelector:@selector(imageSelectorDidiSelectImage:)]) {
  135. NSObject *obj = _imageFileNameArray.count > 0 ? _imageFileNameArray[_selectIndex] : nil;
  136. [_imgDelegete performSelector:@selector(imageSelectorDidiSelectImage:) withObject:obj];
  137. }
  138. [self deleteOtherImage];
  139. [self actionQuit];
  140. }
  141. -(void)deleteOtherImage{
  142. for (int i=0; i<_imageFileNameArray.count; i++) {
  143. if (i != _selectIndex) {
  144. BOOL b = [[NSFileManager defaultManager] removeItemAtPath:_imageFileNameArray[i] error:nil];
  145. if (!b)
  146. NSLog(@"文件删除失败");
  147. }
  148. }
  149. }
  150. -(void)didSelectView:(UIView*)sender{
  151. [self changeCell:_selectIndex selected:NO];
  152. [self changeCell:sender.tag selected:YES];
  153. _selectIndex = sender.tag;
  154. }
  155. -(void)didImageView:(UIView*)sender{
  156. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.tag inSection:0];
  157. ImageSelectorCollectionCell * cell = (ImageSelectorCollectionCell*)[_collectionView cellForItemAtIndexPath:indexPath];
  158. _initFrame = [cell.contentView convertRect:cell.imageView.frame toView:self.view];
  159. UIImage * image = [cell.imageView image];
  160. [self showFullScreenViewImage:image];
  161. }
  162. -(void)changeCell:(long)index selected:(BOOL)value{
  163. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
  164. ImageSelectorCollectionCell * cell = (ImageSelectorCollectionCell*)[_collectionView cellForItemAtIndexPath:indexPath];
  165. cell.isSelected = value;
  166. if(value)
  167. cell.selectView.image = [UIImage imageNamed:@"selected_true"];
  168. else
  169. cell.selectView.image = [UIImage imageNamed:@"selected_fause"];
  170. }
  171. @end