ImageBrowserViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. //
  2. // ImageBrowserViewController.m
  3. // ImageBrowser
  4. //
  5. // Created by msk on 16/9/1.
  6. // Copyright © 2016年 msk. All rights reserved.
  7. //
  8. #import "ImageBrowserViewController.h"
  9. #import "PhotoView.h"
  10. #import "JXUserInfoVC.h"
  11. #import "webpageVC.h"
  12. #import "JXActionSheetVC.h"
  13. #import "KKImageEditorViewController.h"
  14. @interface ImageBrowserViewController ()<UIScrollViewDelegate,PhotoViewDelegate,JXActionSheetVCDelegate,KKImageEditorDelegate,UINavigationControllerDelegate>{
  15. NSMutableArray *_subViewArray;//scrollView的所有子视图
  16. }
  17. /** 背景容器视图 */
  18. @property(nonatomic,strong) UIScrollView *scrollView;
  19. /** 外部操作控制器 */
  20. @property (nonatomic,weak) UIViewController *handleVC;
  21. /** 图片浏览方式 */
  22. @property (nonatomic,assign) PhotoBroswerVCType type;
  23. /** 图片数组 */
  24. @property (nonatomic,strong) NSArray *imagesArray;
  25. /** 初始显示的index */
  26. @property (nonatomic,assign) NSUInteger index;
  27. /** 圆点指示器 */
  28. //@property(nonatomic,strong) UIPageControl *pageControl;
  29. /** 记录当前的图片显示视图 */
  30. @property(nonatomic,strong) PhotoView *photoView;
  31. @end
  32. static ImageBrowserViewController *shared;
  33. @implementation ImageBrowserViewController
  34. //+ (ImageBrowserViewController*)sharedInstance{
  35. // static dispatch_once_t onceToken;
  36. // dispatch_once(&onceToken, ^{
  37. // shared = [[ImageBrowserViewController alloc]init];
  38. // });
  39. // return shared;
  40. //}
  41. - (void)dealloc{
  42. [g_notify removeObserver:self name:kImageDidTouchEndNotification object:[self.imagesArray lastObject]];
  43. }
  44. -(instancetype)init{
  45. self=[super init];
  46. if (self) {
  47. _subViewArray = [NSMutableArray arrayWithCapacity:0];
  48. }
  49. return self;
  50. }
  51. - (void)viewDidLoad {
  52. [super viewDidLoad];
  53. // Do any additional setup after loading the view.
  54. self.view.backgroundColor=[UIColor blackColor];
  55. //去除自动处理
  56. self.automaticallyAdjustsScrollViewInsets = NO;
  57. self.modalPresentationStyle = UIModalPresentationFullScreen;
  58. UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideCurrentVC:)];
  59. [self.view addGestureRecognizer:tap];//为当前view添加手势,隐藏当前显示窗口
  60. }
  61. -(void)hideCurrentVC:(UIGestureRecognizer *)tap{
  62. [self hideScanImageVC];
  63. }
  64. #pragma mark - 显示图片
  65. -(void)loadPhote:(NSInteger)index{
  66. if (index<0 || index >=self.imagesArray.count) {
  67. return;
  68. }
  69. id currentPhotoView = [_subViewArray objectAtIndex:index];
  70. if (![currentPhotoView isKindOfClass:[PhotoView class]]) {
  71. //url数组或图片数组
  72. CGRect frame = CGRectMake(index*_scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
  73. if ([[self.imagesArray firstObject] isKindOfClass:[UIImage class]]) {
  74. PhotoView *photoV = [[PhotoView alloc] initWithFrame:frame withPhotoImage:[self.imagesArray objectAtIndex:index]];
  75. photoV.delegate = self;
  76. [self.scrollView insertSubview:photoV atIndex:0];
  77. [_subViewArray replaceObjectAtIndex:index withObject:photoV];
  78. self.photoView=photoV;
  79. }else if ([[self.imagesArray firstObject] isKindOfClass:[NSString class]]){
  80. PhotoView *photoV = [[PhotoView alloc] initWithFrame:frame withPhotoUrl:[self.imagesArray objectAtIndex:index]];
  81. photoV.delegate = self;
  82. [self.scrollView insertSubview:photoV atIndex:0];
  83. [_subViewArray replaceObjectAtIndex:index withObject:photoV];
  84. self.photoView=photoV;
  85. }
  86. }
  87. }
  88. #pragma mark - 生成显示窗口
  89. +(void)show:(UIViewController *)handleVC delegate:(id)delegate isReadDel:(BOOL)isReadDel type:(PhotoBroswerVCType)type contentArray:(NSMutableArray *)contentArray index:(NSUInteger)index imagesBlock:(NSArray *(^)())imagesBlock{
  90. NSArray *photoModels = imagesBlock();//取出相册数组
  91. if(photoModels == nil || photoModels.count == 0) {
  92. return ;
  93. }
  94. ImageBrowserViewController *imgBrowserVC = [[ImageBrowserViewController alloc] init];
  95. imgBrowserVC.isShow = YES;
  96. if(index >= photoModels.count){
  97. return ;
  98. }
  99. imgBrowserVC.delegate = delegate;
  100. imgBrowserVC.contentArray = contentArray;
  101. imgBrowserVC.index = index;
  102. imgBrowserVC.imagesArray = photoModels;
  103. imgBrowserVC.type =type;
  104. imgBrowserVC.handleVC = handleVC;
  105. imgBrowserVC.isReadDel = isReadDel;
  106. [imgBrowserVC show]; //展示
  107. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  108. [UIApplication sharedApplication].statusBarHidden = YES;
  109. });
  110. }
  111. /** 真正展示 */
  112. -(void)show{
  113. switch (_type) {
  114. case PhotoBroswerVCTypePush://push
  115. [self pushPhotoVC];
  116. break;
  117. case PhotoBroswerVCTypeModal://modal
  118. [self modalPhotoVC];
  119. break;
  120. case PhotoBroswerVCTypeZoom://zoom
  121. [self zoomPhotoVC];
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. /** push */
  128. -(void)pushPhotoVC{
  129. [_handleVC.navigationController pushViewController:self animated:YES];
  130. }
  131. /** modal */
  132. -(void)modalPhotoVC{
  133. [_handleVC presentViewController:self animated:YES completion:nil];
  134. }
  135. /** zoom */
  136. -(void)zoomPhotoVC{
  137. //拿到window
  138. UIWindow *window = _handleVC.view.window;
  139. if(window == nil){
  140. NSLog(@"错误:窗口为空!");
  141. return;
  142. }
  143. self.view.frame=[UIScreen mainScreen].bounds;
  144. [window addSubview:self.view]; //添加视图
  145. [_handleVC addChildViewController:self]; //添加子控制器
  146. }
  147. #pragma mark - 隐藏当前显示窗口
  148. -(void)hideScanImageVC{
  149. if (self.delegate && [self.delegate respondsToSelector:@selector(dismissImageBrowserVC)]) {
  150. [self.delegate dismissImageBrowserVC];
  151. }
  152. [_subViewArray removeAllObjects];
  153. switch (_type) {
  154. case PhotoBroswerVCTypePush://push
  155. [self.navigationController popViewControllerAnimated:YES];
  156. break;
  157. case PhotoBroswerVCTypeModal://modal
  158. [g_notify postNotificationName:kImageDidTouchEndNotification object:self.contentArray[self.index]];
  159. [self dismissViewControllerAnimated:YES completion:nil];
  160. self.scrollView = nil;
  161. self.photoView = nil;
  162. break;
  163. case PhotoBroswerVCTypeZoom://zoom
  164. [self.view removeFromSuperview];
  165. [self removeFromParentViewController];
  166. break;
  167. default:
  168. break;
  169. }
  170. }
  171. #pragma mark - UIScrollViewDelegate
  172. -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  173. CGFloat pageWidth = scrollView.frame.size.width;
  174. NSInteger page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
  175. if (page<0||page>=self.imagesArray.count) {
  176. return;
  177. }
  178. // self.pageControl.currentPage = page;
  179. for (UIView *view in scrollView.subviews) {
  180. if ([view isKindOfClass:[PhotoView class]]) {
  181. PhotoView *photoV=(PhotoView *)[_subViewArray objectAtIndex:page];
  182. if (photoV!=self.photoView) {
  183. [self.photoView.scrollView setZoomScale:1.0 animated:YES];
  184. self.photoView=photoV;
  185. }
  186. }
  187. }
  188. [self loadPhote:page];
  189. }
  190. #pragma mark - PhotoViewDelegate
  191. -(void)tapHiddenPhotoView{
  192. [UIApplication sharedApplication].statusBarHidden = NO;
  193. [self hideScanImageVC];//隐藏当前显示窗口
  194. }
  195. - (void)longPressPhotoView:(UIImage *)image {
  196. [self setupActionSheet];
  197. }
  198. - (void)setupActionSheet {
  199. NSMutableArray *names = @[Localized(@"JX_IdentifyTheQrCode"),Localized(@"JX_ImageEditTitle"),Localized(@"ImageBrowser_save")].mutableCopy;
  200. if (self.isReadDel) {
  201. [names removeObject:Localized(@"ImageBrowser_save")];
  202. [names removeObject:Localized(@"JX_ImageEditTitle")];
  203. }
  204. JXActionSheetVC *actionVC = [[JXActionSheetVC alloc] initWithImages:@[] names:names];
  205. actionVC.delegate = self;
  206. [self presentViewController:actionVC animated:NO completion:nil];
  207. }
  208. - (void)actionSheet:(JXActionSheetVC *)actionSheet didButtonWithIndex:(NSInteger)index {
  209. if (self.isReadDel) {
  210. [self distinguishQRCode];
  211. }else {
  212. if (index == 0) {
  213. [self distinguishQRCode];
  214. }else if (index == 1) {
  215. KKImageEditorViewController *editor = [[KKImageEditorViewController alloc] initWithImage:self.photoView.imageView.image delegate:self];
  216. UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:editor];
  217. [self presentViewController:vc animated:YES completion:nil];
  218. }else if (index == 2) {
  219. [self saveImageToPhotos:self.photoView.imageView.image];
  220. }
  221. }
  222. }
  223. #pragma mark- 照片编辑后的回调
  224. - (void)imageDidFinishEdittingWithImage:(UIImage *)image asset:(PHAsset *)asset
  225. {
  226. self.photoView.imageView.image = image;
  227. [self setupActionSheet];
  228. }
  229. - (void) distinguishQRCode {
  230. UIImageView*tempImageView=(UIImageView*)self.photoView.imageView;
  231. if(tempImageView.image){
  232. //1. 初始化扫描仪,设置设别类型和识别质量
  233. CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];
  234. //2. 扫描获取的特征组
  235. NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:tempImageView.image.CGImage]];
  236. //3. 获取扫描结果
  237. if (features.count <= 0) {
  238. [g_App showAlert:Localized(@"JX_NoQrCode")];
  239. return;
  240. }
  241. CIQRCodeFeature *feature = [features objectAtIndex:0];
  242. NSString *scannedResult = feature.messageString;
  243. if ([self.delegate respondsToSelector:@selector(imageBrowserVCQRCodeAction:)]) {
  244. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  245. [self hideScanImageVC];
  246. [self.delegate imageBrowserVCQRCodeAction:scannedResult];
  247. });
  248. }
  249. // [self QRCodeAction:scannedResult];
  250. // UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:scannedResult delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  251. //
  252. // [alertView show];
  253. }else {
  254. UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:Localized(@"JX_ScanResults") message:Localized(@"JX_Haven'tQrCode") delegate:nil cancelButtonTitle:Localized(@"JX_Confirm") otherButtonTitles:nil, nil];
  255. [alertView show];
  256. }
  257. }
  258. - (void)saveImageToPhotos:(UIImage*)savedImage
  259. {
  260. UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
  261. }
  262. // 指定回调方法
  263. - (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
  264. {
  265. NSString *msg = nil ;
  266. if(error != NULL){
  267. msg = Localized(@"ImageBrowser_saveFaild");
  268. }else{
  269. msg = Localized(@"ImageBrowser_saveSuccess");
  270. }
  271. [g_server showMsg:msg];
  272. }
  273. #pragma mark - 懒加载
  274. -(UIScrollView *)scrollView{
  275. if (_scrollView==nil) {
  276. _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT)];
  277. _scrollView.delegate=self;
  278. _scrollView.pagingEnabled=YES;
  279. _scrollView.contentOffset=CGPointZero;
  280. //设置最大伸缩比例
  281. _scrollView.maximumZoomScale=3;
  282. //设置最小伸缩比例
  283. _scrollView.minimumZoomScale=1;
  284. _scrollView.showsHorizontalScrollIndicator = NO;
  285. _scrollView.showsVerticalScrollIndicator = NO;
  286. [self.view addSubview:_scrollView];
  287. }
  288. return _scrollView;
  289. }
  290. //-(UIPageControl *)pageControl{
  291. // if (_pageControl==nil) {
  292. // UIView *bottomView=[[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT-40, WIDTH, 30)];
  293. // bottomView.backgroundColor=[UIColor clearColor];
  294. // _pageControl = [[UIPageControl alloc] initWithFrame:bottomView.bounds];
  295. // _pageControl.currentPage = self.index;
  296. // _pageControl.numberOfPages = self.imagesArray.count;
  297. // _pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:153 green:153 blue:153 alpha:1];
  298. // _pageControl.pageIndicatorTintColor = [UIColor colorWithRed:235 green:235 blue:235 alpha:0.6];
  299. // [bottomView addSubview:_pageControl];
  300. // [self.view addSubview:bottomView];
  301. // }
  302. // return _pageControl;
  303. //}
  304. -(void)setImagesArray:(NSArray *)imagesArray{
  305. _imagesArray = imagesArray;
  306. //设置contentSize
  307. self.scrollView.contentSize = CGSizeMake(JX_SCREEN_WIDTH * self.imagesArray.count, 0);
  308. for (int i = 0; i < self.imagesArray.count; i++) {
  309. [_subViewArray addObject:[NSNull class]];
  310. }
  311. self.scrollView.contentOffset = CGPointMake(JX_SCREEN_WIDTH*self.index, 0);//此句代码需放在[_subViewArray addObject:[NSNull class]]之后,因为其主动调用scrollView的代理方法,否则会出现数组越界
  312. if (self.imagesArray.count==1) {
  313. // _pageControl.hidden=YES;
  314. }else{
  315. // self.pageControl.currentPage=self.index;
  316. }
  317. [self loadPhote:self.index];//显示当前索引的图片
  318. }
  319. - (void)didReceiveMemoryWarning {
  320. [super didReceiveMemoryWarning];
  321. // Dispose of any resources that can be recreated.
  322. }
  323. /*
  324. #pragma mark - Navigation
  325. // In a storyboard-based application, you will often want to do a little preparation before navigation
  326. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  327. // Get the new view controller using [segue destinationViewController].
  328. // Pass the selected object to the new view controller.
  329. }
  330. */
  331. @end