ContentPreviewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. //
  2. // NewPhotoPreviewController.m
  3. // RA Image
  4. //
  5. // Created by Jack on 2017/6/14.
  6. // Copyright © 2017年 USAI. All rights reserved.
  7. //
  8. #import "ContentPreviewController.h"
  9. #import "PhotoPreviewCell.h"
  10. #import "VideoPreviewCell.h"
  11. #import "AppDelegate.h"
  12. #import "FileCache.h"
  13. @interface ContentPreviewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  14. @property (nonatomic,assign) NSUInteger currentIndex;
  15. @property (strong, nonatomic) IBOutlet UILabel *indicator;
  16. @property (strong, nonatomic) IBOutlet UICollectionView *previewContainer;
  17. @property (nonatomic,strong) NSArray *photos;
  18. @property (nonatomic,strong) UIView *mask;
  19. @property (nonatomic,assign) BOOL hideNavigationBar;
  20. @end
  21. @implementation ContentPreviewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view.
  25. // self.automaticallyAdjustsScrollViewInsets = NO;
  26. // self.navigationController.navigationBar.translucent = NO;
  27. // self.edgesForExtendedLayout = UIRectEdgeNone;
  28. UIView *v = [UIView new];
  29. [self.view insertSubview:v atIndex:0];
  30. NSMutableArray *tmpArr = [NSMutableArray array];
  31. int count = [[self.content objectForKey:@"count"] intValue];
  32. for (int i = 0; i < count; i++) {
  33. NSDictionary *item = [self.content objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  34. [tmpArr addObject:item];
  35. }
  36. self.photos = [tmpArr copy];
  37. self.indicator.layer.cornerRadius = 20;
  38. self.indicator.layer.masksToBounds = YES;
  39. self.previewContainer.pagingEnabled = YES;
  40. NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
  41. self.indicator.text = offset;
  42. self.hideNavigationBar = self.navigationController.isNavigationBarHidden;
  43. self.navigationController.navigationBarHidden = YES;
  44. // 掩藏滚动
  45. [self.view insertSubview:self.mask belowSubview:self.indicator];
  46. }
  47. - (void)viewWillAppear:(BOOL)animated {
  48. [super viewWillAppear:animated];
  49. }
  50. - (void)viewDidLayoutSubviews {
  51. [super viewDidLayoutSubviews];
  52. // if (self.currentIndex > 0) {
  53. // [self.previewContainer scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:16 animated:YES];
  54. // }
  55. // [self scrollToIndex:self.currentIndex + 1];
  56. [self.previewContainer setContentOffset:CGPointMake((self.currentIndex + 1) * CGRectGetWidth(self.previewContainer.frame), 0) animated:YES];
  57. }
  58. - (void)viewDidAppear:(BOOL)animated {
  59. [super viewDidAppear:animated];
  60. // [self.previewContainer scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:16 animated:YES];
  61. __weak typeof(self) weakself = self;
  62. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  63. sleep(0.25);
  64. dispatch_async(dispatch_get_main_queue(), ^{
  65. [weakself.mask removeFromSuperview];
  66. weakself.mask = nil;
  67. });
  68. });
  69. }
  70. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  71. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  72. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  73. // what ever you want to prepare
  74. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  75. [self.previewContainer.collectionViewLayout invalidateLayout];
  76. }];
  77. }
  78. //- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  79. //
  80. // // 重新布局 Item 大小
  81. // [self.previewContainer.collectionViewLayout invalidateLayout];
  82. //
  83. // // 重新布局Item位置
  84. //// [self.previewContainer scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:32 animated:NO];
  85. //// [self scrollToIndex:self.currentIndex + 1];
  86. //}
  87. - (BOOL)prefersStatusBarHidden {
  88. return YES;
  89. }
  90. - (void)didReceiveMemoryWarning {
  91. [super didReceiveMemoryWarning];
  92. // Dispose of any resources that can be recreated.
  93. }
  94. - (UIView *)mask {
  95. if (!_mask) {
  96. _mask = [[UIView alloc] initWithFrame:self.view.bounds];
  97. _mask.backgroundColor = [UIColor blackColor];
  98. }
  99. return _mask;
  100. }
  101. #pragma mark - Setter
  102. - (void)setOffset:(NSUInteger)offset {
  103. [self setCurrentIndex:offset];
  104. }
  105. //- (void)setCurrentIndex:(NSUInteger)currentIndex {
  106. // _currentIndex = currentIndex;
  107. // NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
  108. // self.indicator.text = offset;
  109. //}
  110. #pragma mark - FlowLayout Delegate
  111. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  112. return collectionView.bounds.size;
  113. }
  114. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  115. return 0;
  116. }
  117. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  118. return 0;
  119. }
  120. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  121. return UIEdgeInsetsZero;
  122. }
  123. #pragma mark - CollectionView Delegate
  124. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  125. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  126. NSDictionary *item = [self.photos objectAtIndex:idx];
  127. NSString *type = [item objectForKey:@"type"];
  128. if ([type isEqualToString:@"image"]) {
  129. PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
  130. [self photoCell:preCell loadImage:item];
  131. } else if ([type isEqualToString:@"video"]) {
  132. VideoPreviewCell *videoCell = (VideoPreviewCell *)cell;
  133. videoCell.item = item;
  134. }
  135. }
  136. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  137. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  138. NSDictionary *item = [self.photos objectAtIndex:idx];
  139. NSString *type = [item objectForKey:@"type"];
  140. if ([type isEqualToString:@"image"]) {
  141. PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
  142. UIScrollView *sc = preCell.scrollView;
  143. sc.zoomScale = 1;
  144. sc.contentSize = CGSizeZero;
  145. sc.contentOffset = CGPointZero;
  146. } else if ([type isEqualToString:@"video"]) {
  147. VideoPreviewCell *videoCell = (VideoPreviewCell *)cell;
  148. [videoCell reset];
  149. }
  150. }
  151. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  152. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  153. NSDictionary *item = [self.photos objectAtIndex:idx];
  154. NSString *type = [item objectForKey:@"type"];
  155. if ([type isEqualToString:@"video"]) {
  156. VideoPreviewCell *videoCell = (VideoPreviewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  157. [videoCell play];
  158. }
  159. }
  160. #pragma mark - CollectionView DataSource
  161. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  162. if ([self.photos count] == 0) {
  163. return 0;
  164. }
  165. return self.photos.count + 2;
  166. }
  167. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  168. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  169. NSDictionary *item = [self.photos objectAtIndex:idx];
  170. NSString *type = [item objectForKey:@"type"];
  171. if ([type isEqualToString:@"image"]) {
  172. PhotoPreviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoPreviewCell" forIndexPath:indexPath];
  173. cell.scrollView.delegate = self;
  174. return cell;
  175. } else if ([type isEqualToString:@"video"]) {
  176. VideoPreviewCell *videoCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VideoPreviewCell" forIndexPath:indexPath];
  177. return videoCell;
  178. }
  179. return nil;
  180. }
  181. #pragma mark - ScrollView Delegate
  182. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  183. // if (scrollView == self.previewContainer) {
  184. //
  185. // CGFloat x = scrollView.contentOffset.x / scrollView.frame.size.width;
  186. // int idx = (int)x;
  187. // if (idx == x) {
  188. // if (self.currentIndex != idx) {
  189. // self.currentIndex = idx;
  190. //
  191. // }
  192. // }
  193. // }
  194. }
  195. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  196. CGFloat offsetX = scrollView.contentOffset.x;
  197. float idxf = offsetX / CGRectGetWidth(scrollView.frame);
  198. int idxi = (int)(offsetX / CGRectGetWidth(scrollView.frame));
  199. if (idxf == idxi) {
  200. if (idxi == 0) {
  201. self.currentIndex = self.photos.count - 1;
  202. } else if (idxi == self.photos.count + 1) {
  203. self.currentIndex = 0;
  204. } else {
  205. self.currentIndex = idxi - 1;
  206. }
  207. // if (self.indicator) {
  208. // self.indicator(self.currentIndex, self.photos.count);
  209. // }
  210. [self updateIndicator];
  211. } else {
  212. // idxi = ceil(idxf);
  213. // if (idxi != 0 || idxi != self.photos.count + 1) {
  214. // [self scrollToIndex:idxi];
  215. // }
  216. }
  217. if (idxi == 0) {
  218. [self scrollToIndex:self.photos.count];
  219. }
  220. if (idxi == self.photos.count + 1) {
  221. [self scrollToIndex:1];
  222. }
  223. }
  224. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  225. if (scrollView != self.previewContainer) {
  226. return scrollView.subviews.firstObject;
  227. }
  228. return nil;
  229. }
  230. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  231. if (self.previewContainer != scrollView) {
  232. }
  233. }
  234. #pragma mark - Private
  235. - (void)updateIndicator {
  236. NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
  237. self.indicator.text = offset;
  238. }
  239. - (NSUInteger)contentOffsetForIndexPath:(NSIndexPath *)indexPath {
  240. NSUInteger idx = indexPath.row;
  241. if (idx == 0) {
  242. idx = self.photos.count - 1;
  243. }else if (idx == self.photos.count + 1) {
  244. idx = 0;
  245. } else {
  246. idx = idx - 1;
  247. }
  248. return idx;
  249. }
  250. - (void)scrollToIndex:(NSUInteger)index { // 不会出现肉眼可见的滚动效果
  251. self.previewContainer.contentOffset = CGPointMake(index * CGRectGetWidth(self.previewContainer.frame), 0);
  252. }
  253. - (void) photoCell:(PhotoPreviewCell *)cell loadImage:(NSDictionary *)item_json {
  254. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  255. NSString* img_url = [item_json valueForKey:@"s"];
  256. NSString* type = item_json[@"type"];
  257. NSString* file_name=[img_url lastPathComponent];
  258. NSData* img_data=nil;
  259. // 加载Image
  260. if([type isEqualToString:@"video"])
  261. {
  262. img_data = UIImagePNGRepresentation([UIImage imageNamed:@"play"]);
  263. }
  264. else
  265. {
  266. if([item_json[@"is_localfile"] boolValue])
  267. img_data = [NSData dataWithContentsOfFile:img_url];
  268. else
  269. img_data = [FileCache load_cached_img:file_name loadFrom:img_url];
  270. }
  271. // 设置Image
  272. if(img_data!=nil)
  273. {
  274. dispatch_async(dispatch_get_main_queue(), ^{
  275. UIImage * img =[UIImage imageWithData:img_data];
  276. [cell setPhoto:img];
  277. });
  278. }
  279. else
  280. {
  281. NSData* downloadimg_data = nil;
  282. BOOL offline_mode = NO;
  283. #ifdef OFFLINE_MODE
  284. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  285. offline_mode = appDelegate.offline_mode;
  286. #endif
  287. if (!offline_mode) {
  288. downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  289. }
  290. dispatch_async(dispatch_get_main_queue(), ^{
  291. if(downloadimg_data!=nil)
  292. {
  293. [FileCache cache_img:downloadimg_data filename:file_name saveTo:img_url];
  294. UIImage * img =[UIImage imageWithData:downloadimg_data];
  295. [cell setPhoto:img];
  296. }
  297. else
  298. {
  299. UIImage * img =[UIImage imageNamed:@"notfound_l"];
  300. [cell setPhoto:img];
  301. }
  302. });
  303. }
  304. });
  305. }
  306. - (IBAction)closeBtnClick:(UIButton *)sender {
  307. if (self.navigationController) {
  308. self.navigationController.navigationBarHidden = self.hideNavigationBar;
  309. [self.navigationController popViewControllerAnimated:YES];
  310. } else {
  311. [self dismissViewControllerAnimated:YES completion:nil];
  312. }
  313. }
  314. @end