ContentPreviewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  71. // 重新布局 Item 大小
  72. [self.previewContainer.collectionViewLayout invalidateLayout];
  73. // 重新布局Item位置
  74. // [self.previewContainer scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:32 animated:NO];
  75. // [self scrollToIndex:self.currentIndex + 1];
  76. }
  77. - (BOOL)prefersStatusBarHidden {
  78. return YES;
  79. }
  80. - (void)didReceiveMemoryWarning {
  81. [super didReceiveMemoryWarning];
  82. // Dispose of any resources that can be recreated.
  83. }
  84. - (UIView *)mask {
  85. if (!_mask) {
  86. _mask = [[UIView alloc] initWithFrame:self.view.bounds];
  87. _mask.backgroundColor = [UIColor blackColor];
  88. }
  89. return _mask;
  90. }
  91. #pragma mark - Setter
  92. - (void)setOffset:(NSUInteger)offset {
  93. [self setCurrentIndex:offset];
  94. }
  95. //- (void)setCurrentIndex:(NSUInteger)currentIndex {
  96. // _currentIndex = currentIndex;
  97. // NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
  98. // self.indicator.text = offset;
  99. //}
  100. #pragma mark - FlowLayout Delegate
  101. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  102. return collectionView.bounds.size;
  103. }
  104. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  105. return 0;
  106. }
  107. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  108. return 0;
  109. }
  110. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  111. return UIEdgeInsetsZero;
  112. }
  113. #pragma mark - CollectionView Delegate
  114. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  115. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  116. NSDictionary *item = [self.photos objectAtIndex:idx];
  117. NSString *type = [item objectForKey:@"type"];
  118. if ([type isEqualToString:@"image"]) {
  119. PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
  120. [self photoCell:preCell loadImage:item];
  121. } else if ([type isEqualToString:@"video"]) {
  122. VideoPreviewCell *videoCell = (VideoPreviewCell *)cell;
  123. videoCell.item = item;
  124. }
  125. }
  126. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  127. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  128. NSDictionary *item = [self.photos objectAtIndex:idx];
  129. NSString *type = [item objectForKey:@"type"];
  130. if ([type isEqualToString:@"image"]) {
  131. PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
  132. UIScrollView *sc = preCell.scrollView;
  133. sc.zoomScale = 1;
  134. sc.contentSize = CGSizeZero;
  135. sc.contentOffset = CGPointZero;
  136. } else if ([type isEqualToString:@"video"]) {
  137. VideoPreviewCell *videoCell = (VideoPreviewCell *)cell;
  138. [videoCell reset];
  139. }
  140. }
  141. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  142. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  143. NSDictionary *item = [self.photos objectAtIndex:idx];
  144. NSString *type = [item objectForKey:@"type"];
  145. if ([type isEqualToString:@"video"]) {
  146. VideoPreviewCell *videoCell = (VideoPreviewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  147. [videoCell play];
  148. }
  149. }
  150. #pragma mark - CollectionView DataSource
  151. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  152. if ([self.photos count] == 0) {
  153. return 0;
  154. }
  155. return self.photos.count + 2;
  156. }
  157. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  158. NSUInteger idx = [self contentOffsetForIndexPath:indexPath];
  159. NSDictionary *item = [self.photos objectAtIndex:idx];
  160. NSString *type = [item objectForKey:@"type"];
  161. if ([type isEqualToString:@"image"]) {
  162. PhotoPreviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoPreviewCell" forIndexPath:indexPath];
  163. cell.scrollView.delegate = self;
  164. return cell;
  165. } else if ([type isEqualToString:@"video"]) {
  166. VideoPreviewCell *videoCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VideoPreviewCell" forIndexPath:indexPath];
  167. return videoCell;
  168. }
  169. return nil;
  170. }
  171. #pragma mark - ScrollView Delegate
  172. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  173. // if (scrollView == self.previewContainer) {
  174. //
  175. // CGFloat x = scrollView.contentOffset.x / scrollView.frame.size.width;
  176. // int idx = (int)x;
  177. // if (idx == x) {
  178. // if (self.currentIndex != idx) {
  179. // self.currentIndex = idx;
  180. //
  181. // }
  182. // }
  183. // }
  184. }
  185. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  186. CGFloat offsetX = scrollView.contentOffset.x;
  187. float idxf = offsetX / CGRectGetWidth(scrollView.frame);
  188. int idxi = (int)(offsetX / CGRectGetWidth(scrollView.frame));
  189. if (idxf == idxi) {
  190. if (idxi == 0) {
  191. self.currentIndex = self.photos.count - 1;
  192. } else if (idxi == self.photos.count + 1) {
  193. self.currentIndex = 0;
  194. } else {
  195. self.currentIndex = idxi - 1;
  196. }
  197. // if (self.indicator) {
  198. // self.indicator(self.currentIndex, self.photos.count);
  199. // }
  200. [self updateIndicator];
  201. } else {
  202. // idxi = ceil(idxf);
  203. // if (idxi != 0 || idxi != self.photos.count + 1) {
  204. // [self scrollToIndex:idxi];
  205. // }
  206. }
  207. if (idxi == 0) {
  208. [self scrollToIndex:self.photos.count];
  209. }
  210. if (idxi == self.photos.count + 1) {
  211. [self scrollToIndex:1];
  212. }
  213. }
  214. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  215. if (scrollView != self.previewContainer) {
  216. return scrollView.subviews.firstObject;
  217. }
  218. return nil;
  219. }
  220. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  221. if (self.previewContainer != scrollView) {
  222. }
  223. }
  224. #pragma mark - Private
  225. - (void)updateIndicator {
  226. NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
  227. self.indicator.text = offset;
  228. }
  229. - (NSUInteger)contentOffsetForIndexPath:(NSIndexPath *)indexPath {
  230. NSUInteger idx = indexPath.row;
  231. if (idx == 0) {
  232. idx = self.photos.count - 1;
  233. }else if (idx == self.photos.count + 1) {
  234. idx = 0;
  235. } else {
  236. idx = idx - 1;
  237. }
  238. return idx;
  239. }
  240. - (void)scrollToIndex:(NSUInteger)index { // 不会出现肉眼可见的滚动效果
  241. self.previewContainer.contentOffset = CGPointMake(index * CGRectGetWidth(self.previewContainer.frame), 0);
  242. }
  243. - (void) photoCell:(PhotoPreviewCell *)cell loadImage:(NSDictionary *)item_json {
  244. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  245. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  246. NSString* img_url = [item_json valueForKey:@"s"];
  247. NSString* type = item_json[@"type"];
  248. NSString* file_name=[img_url lastPathComponent];
  249. NSData* img_data=nil;
  250. // 加载Image
  251. if([type isEqualToString:@"video"])
  252. {
  253. img_data = UIImagePNGRepresentation([UIImage imageNamed:@"play"]);
  254. }
  255. else
  256. {
  257. if([item_json[@"is_localfile"] boolValue])
  258. img_data = [NSData dataWithContentsOfFile:img_url];
  259. else
  260. img_data = [FileCache load_cached_img:file_name loadFrom:img_url];
  261. }
  262. // 设置Image
  263. if(img_data!=nil)
  264. {
  265. dispatch_async(dispatch_get_main_queue(), ^{
  266. UIImage * img =[UIImage imageWithData:img_data];
  267. [cell setPhoto:img];
  268. });
  269. }
  270. else
  271. {
  272. NSData* downloadimg_data = nil;
  273. BOOL offline_mode = NO;
  274. #ifdef OFFLINE_MODE
  275. offline_mode = appDelegate.offline_mode;
  276. #endif
  277. if (!offline_mode) {
  278. downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  279. }
  280. dispatch_async(dispatch_get_main_queue(), ^{
  281. if(downloadimg_data!=nil)
  282. {
  283. [FileCache cache_img:downloadimg_data filename:file_name saveTo:img_url];
  284. UIImage * img =[UIImage imageWithData:downloadimg_data];
  285. [cell setPhoto:img];
  286. }
  287. else
  288. {
  289. UIImage * img =[UIImage imageNamed:@"notfound_l"];
  290. [cell setPhoto:img];
  291. }
  292. });
  293. }
  294. });
  295. }
  296. - (IBAction)closeBtnClick:(UIButton *)sender {
  297. if (self.navigationController) {
  298. self.navigationController.navigationBarHidden = self.hideNavigationBar;
  299. [self.navigationController popViewControllerAnimated:YES];
  300. } else {
  301. [self dismissViewControllerAnimated:YES completion:nil];
  302. }
  303. }
  304. @end