YBImageView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // YBImageView.m
  3. // yunbaolive
  4. //
  5. // Created by IOS1 on 2019/3/1.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "YBImageView.h"
  9. #import "YBShowBigImageView.h"
  10. #import "SDWebImageDownloader.h"
  11. @interface YBImageView ()<UIScrollViewDelegate>{
  12. NSInteger currentIndex;
  13. UILabel *indexLb;
  14. UIButton *deleteBtn;
  15. UIView *navi;
  16. UIView *_actionSheetView;
  17. BOOL isMine;
  18. BOOL isfromDt;
  19. }
  20. @property (nonatomic,copy) YBImageViewBlock returnBlock;
  21. @end
  22. @implementation YBImageView{
  23. UITapGestureRecognizer *tap;
  24. UIScrollView *backScrollV;
  25. NSMutableArray *imageArray;
  26. NSMutableArray *imgViewArray;
  27. }
  28. - (instancetype)initWithImageArray:(NSArray *)array andIndex:(NSInteger)index andMine:(BOOL)ismine isDtCell:(BOOL)isDt andBlock:(nonnull YBImageViewBlock)block{
  29. self = [super init];
  30. isMine = ismine;
  31. isfromDt = isDt;
  32. imageArray = [array mutableCopy];
  33. currentIndex = index;
  34. self.returnBlock = block;
  35. self.frame = CGRectMake(0, 0, _window_width, _window_height);
  36. if (self) {
  37. self.userInteractionEnabled = YES;
  38. tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showHideNavi)];
  39. [self addGestureRecognizer:tap];
  40. backScrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(_window_width/2, _window_height/2, 0, 0)];
  41. backScrollV.backgroundColor = [UIColor blackColor];
  42. backScrollV.contentSize = CGSizeMake(_window_width*imageArray.count, 0);
  43. backScrollV.contentOffset = CGPointMake(_window_width * index, 0);
  44. backScrollV.delegate = self;
  45. backScrollV.pagingEnabled=YES;
  46. //设置最大伸缩比例
  47. backScrollV.maximumZoomScale=1;
  48. //设置最小伸缩比例
  49. backScrollV.minimumZoomScale=1;
  50. backScrollV.showsHorizontalScrollIndicator = NO;
  51. backScrollV.showsVerticalScrollIndicator = NO;
  52. [self addSubview:backScrollV];
  53. imgViewArray = [NSMutableArray array];
  54. for (int i = 0; i < imageArray.count; i++) {
  55. id imageContent = imageArray[i];
  56. YBShowBigImageView *imgV = [[YBShowBigImageView alloc]initWithFrame:CGRectMake(_window_width*i, 0, _window_width, _window_height)];
  57. if ([imageContent isKindOfClass:[UIImage class]]) {
  58. imgV.imageView.image = imageContent;
  59. }else if ([imageContent isKindOfClass:[NSString class]]){
  60. [imgV.imageView sd_setImageWithURL:[NSURL URLWithString:imageContent]];
  61. }else if ([imageContent isKindOfClass:[NSDictionary class]]){
  62. [imgV.imageView sd_setImageWithURL:[NSURL URLWithString:minstr([imageContent valueForKey:@"thumb"])]];
  63. }
  64. [backScrollV addSubview:imgV];
  65. [imgViewArray addObject:imgV];
  66. }
  67. if (isfromDt) {
  68. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];//初始化一个长按手势
  69. [longPress setMinimumPressDuration:1];//设置按多久之后触发事件
  70. [backScrollV addGestureRecognizer:longPress];//把长按手势添加给按钮
  71. }
  72. [self showBigView];
  73. [self creatNavi];
  74. }
  75. return self;
  76. }
  77. -(void)longPressAction:(UILongPressGestureRecognizer*)sender
  78. {
  79. NSLog(@"currentindex-----长安----:%ld",currentIndex);
  80. if (sender.state == UIGestureRecognizerStateBegan) {
  81. [self showActionView];
  82. }
  83. }
  84. -(void)showActionView{
  85. if (_actionSheetView) {
  86. [_actionSheetView removeFromSuperview];
  87. _actionSheetView = nil;
  88. }
  89. _actionSheetView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  90. _actionSheetView.backgroundColor = [UIColor clearColor];
  91. [self addSubview:_actionSheetView];
  92. UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, _window_height-150, _window_width, 150)];
  93. backView.backgroundColor = [UIColor clearColor];
  94. [_actionSheetView addSubview:backView];
  95. UIButton *btn1 = [UIButton buttonWithType:0];
  96. btn1.frame= CGRectMake(10, 10, _window_width-20, 55);
  97. [btn1 setBackgroundColor:[UIColor whiteColor]];
  98. [btn1 setTitle:YZMsg(@"保存到本地相册") forState:0];
  99. [btn1 setTitleColor:[UIColor blackColor] forState:0];
  100. btn1.titleLabel.font = [UIFont systemFontOfSize:14];
  101. btn1.layer.cornerRadius = 5;
  102. btn1.layer.masksToBounds = YES;
  103. [btn1 addTarget:self action:@selector(saveImgClick) forControlEvents:UIControlEventTouchUpInside];
  104. [backView addSubview:btn1];
  105. UIButton *btn2 = [UIButton buttonWithType:0];
  106. btn2.frame= CGRectMake(10, btn1.bottom+10, _window_width-20, 55);
  107. [btn2 setBackgroundColor:[UIColor whiteColor]];
  108. [btn2 setTitle:YZMsg(@"取消") forState:0];
  109. [btn2 addTarget:self action:@selector(cancleClick) forControlEvents:UIControlEventTouchUpInside];
  110. [btn2 setTitleColor:[UIColor blackColor] forState:0];
  111. btn2.titleLabel.font = [UIFont systemFontOfSize:14];
  112. btn2.layer.cornerRadius = 5;
  113. btn2.layer.masksToBounds = YES;
  114. [backView addSubview:btn2];
  115. }
  116. -(void)cancleClick{
  117. if (_actionSheetView) {
  118. [_actionSheetView removeFromSuperview];
  119. _actionSheetView = nil;
  120. }
  121. }
  122. -(void)saveImgClick{
  123. YBShowBigImageView *imgV = imgViewArray[currentIndex];
  124. UIImageWriteToSavedPhotosAlbum(imgV.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
  125. }
  126. //指定回调方法
  127. - (void)image: (UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
  128. if (image == nil) {
  129. return;
  130. }
  131. NSString *msg;
  132. if(error != NULL){
  133. msg = @"保存图片失败" ;
  134. }else{
  135. msg = @"保存图片成功";
  136. }
  137. [MBProgressHUD showError:msg];
  138. NSLog(@"🌹🌹🌹🌹%@",msg);
  139. [self cancleClick];
  140. }
  141. - (void)showBigView{
  142. [UIView animateWithDuration:0.2 animations:^{
  143. backScrollV.frame = CGRectMake(0, 0, _window_width, _window_height);
  144. }];
  145. }
  146. - (void)doreturn{
  147. [UIView animateWithDuration:0.2 animations:^{
  148. backScrollV.frame = CGRectMake(_window_width/2, _window_height/2, 0, 0);
  149. }completion:^(BOOL finished) {
  150. if (self.returnBlock) {
  151. self.returnBlock(imageArray);
  152. }
  153. [backScrollV removeFromSuperview];
  154. backScrollV = nil;
  155. [self removeFromSuperview];
  156. }];
  157. }
  158. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  159. currentIndex = scrollView.contentOffset.x/_window_width;
  160. indexLb.text = [NSString stringWithFormat:@"%ld/%ld",currentIndex+1,imageArray.count];
  161. }
  162. -(void)creatNavi {
  163. navi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 64+statusbarHeight)];
  164. navi.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  165. [self addSubview:navi];
  166. UIButton *retrunBtn = [UIButton buttonWithType:0];
  167. retrunBtn.frame = CGRectMake(10, 25+statusbarHeight, 30, 30);
  168. [retrunBtn setImage:[UIImage imageNamed:@"pub_back"] forState:0];
  169. [retrunBtn addTarget:self action:@selector(doreturn) forControlEvents:UIControlEventTouchUpInside];
  170. [navi addSubview:retrunBtn];
  171. UILabel *titleL = [[UILabel alloc]init];
  172. titleL.frame = CGRectMake(_window_width/2-40, 25+statusbarHeight, 80, 30);
  173. titleL.textColor = [UIColor whiteColor];
  174. titleL.font = [UIFont systemFontOfSize:16];
  175. titleL.textAlignment = NSTextAlignmentCenter;
  176. titleL.text = YZMsg(@"预览");
  177. [navi addSubview:titleL];
  178. indexLb = [[UILabel alloc]init];
  179. indexLb.frame = CGRectMake(_window_width-100, 22+statusbarHeight, 80, 30);
  180. indexLb.textColor = [UIColor whiteColor];
  181. indexLb.font = [UIFont systemFontOfSize:15];
  182. indexLb.textAlignment = NSTextAlignmentRight;
  183. indexLb.text = [NSString stringWithFormat:@"%ld/%ld",currentIndex+1,imageArray.count];
  184. [navi addSubview:indexLb];
  185. id imageContent = [imageArray firstObject];
  186. if ([imageContent isKindOfClass:[UIImage class]] || isMine) {
  187. deleteBtn = [UIButton buttonWithType:0];
  188. deleteBtn.frame = CGRectMake(0, _window_height-40, _window_width, 40);
  189. deleteBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  190. [deleteBtn addTarget:self action:@selector(deleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
  191. [deleteBtn setTitle:YZMsg(@"删除") forState:0];
  192. [deleteBtn setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.5]];
  193. [self addSubview:deleteBtn];
  194. }
  195. // [PublicObj lineViewWithFrame:CGRectMake(0, 63+statusbarHeight, _window_width, 1) andColor:colorf3 andView:navi];
  196. }
  197. -(void)deleteBtnClick{
  198. if (isMine) {
  199. [MBProgressHUD showMessage:@""];
  200. NSString *thumbID = minstr([imageArray[currentIndex] valueForKey:@"id"]);
  201. NSMutableDictionary *mudic = @{
  202. @"uid":[Config getOwnID],
  203. @"token":[Config getOwnToken],
  204. @"id":thumbID,
  205. }.mutableCopy;
  206. NSString *sign = [PublicObj sortString:mudic];
  207. [mudic setObject:sign forKey:@"sign"];
  208. [YBNetworking postWithUrl:@"Photo.DelPhoto" Dic:mudic Suc:^(int code, id info, NSString *msg) {
  209. [MBProgressHUD hideHUD];
  210. [MBProgressHUD showError:msg];
  211. if (code == 0) {
  212. [self deleteSucess];
  213. }
  214. } Fail:^(id fail) {
  215. [MBProgressHUD hideHUD];
  216. }];
  217. }else{
  218. [self deleteSucess];
  219. }
  220. }
  221. - (void)deleteSucess{
  222. [imageArray removeObjectAtIndex:currentIndex];
  223. if (imageArray.count == 0) {
  224. [self doreturn];
  225. }else{
  226. UIImageView *imgV = imgViewArray[currentIndex];
  227. [imgV removeFromSuperview];
  228. [imgViewArray removeObjectAtIndex:currentIndex];
  229. if (currentIndex == 0) {
  230. currentIndex = 0;
  231. }else{
  232. currentIndex -= 1;
  233. }
  234. indexLb.text = [NSString stringWithFormat:@"%ld/%ld",currentIndex+1,imageArray.count];
  235. backScrollV.contentSize = CGSizeMake(_window_width*imageArray.count, 0);
  236. [backScrollV setContentOffset:CGPointMake(_window_width*currentIndex, 0)];
  237. for (int i = 0; i < imgViewArray.count; i ++) {
  238. YBShowBigImageView *imgVVVV = imgViewArray[i];
  239. imgVVVV.x = _window_width * i;
  240. }
  241. }
  242. }
  243. - (void)showHideNavi{
  244. navi.hidden = !navi.hidden;
  245. if (deleteBtn) {
  246. deleteBtn.hidden = navi.hidden;
  247. }
  248. }
  249. -(void)hideDelete{
  250. deleteBtn.hidden = YES;
  251. }
  252. @end