CommonEditorBannerCell.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // CommonEditorBannerCell.m
  3. // TestPhoto
  4. //
  5. // Created by Jack on 2017/11/30.
  6. // Copyright © 2017年 Jack. All rights reserved.
  7. //
  8. #import "CommonEditorBannerCell.h"
  9. #import "CommonEditorBannerItemCell.h"
  10. #import "RAYTPlayer.h"
  11. #import "FileCache.h"
  12. @interface CommonEditorBannerCell () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  13. {
  14. NSMutableDictionary *_contentJson;
  15. }
  16. @property (weak, nonatomic) IBOutlet RAYTPlayer *ytplayerView;
  17. @property (strong, nonatomic) IBOutlet UIPageControl *pageControlView;
  18. @property (strong, nonatomic) IBOutlet UICollectionView *bannerCollectionView;
  19. @property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;
  20. @end
  21. @implementation CommonEditorBannerCell
  22. - (void)awakeFromNib {
  23. [super awakeFromNib];
  24. // Initialization code
  25. [self configBanner];
  26. }
  27. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  28. if (self = [super initWithCoder:aDecoder]) {
  29. [self configBanner];
  30. }
  31. return self;
  32. }
  33. - (instancetype)initWithFrame:(CGRect)frame {
  34. if (self = [super initWithFrame:frame]) {
  35. [self configBanner];
  36. }
  37. return self;
  38. }
  39. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  40. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  41. [self configBanner];
  42. }
  43. return self;
  44. }
  45. #pragma mark -
  46. - (void)configBanner {
  47. if (self.bannerCollectionView) {
  48. [self.bannerCollectionView registerNib:[UINib nibWithNibName:@"Phone_Banner_Item_Cell" bundle:nil] forCellWithReuseIdentifier:@"CommonEditorBannerItemCell"];
  49. self.flowLayout = (UICollectionViewFlowLayout *)self.bannerCollectionView.collectionViewLayout;
  50. }
  51. }
  52. - (void)layoutSubviews {
  53. [super layoutSubviews];
  54. [self.flowLayout invalidateLayout];
  55. }
  56. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  57. [super setSelected:selected animated:animated];
  58. // Configure the view for the selected state
  59. }
  60. #pragma mark - Interface
  61. - (NSDictionary *)content {
  62. return _contentJson;
  63. }
  64. - (void)setContent:(NSDictionary *)content {
  65. _contentJson = [content mutableCopy];
  66. int count = [[_contentJson objectForKey:@"count"] intValue];
  67. self.pageControlView.numberOfPages = count;
  68. self.pageControlView.currentPage = 0;
  69. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  70. for(int i=0;i<count;i++)
  71. {
  72. NSDictionary* item_json=[_contentJson objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  73. NSString* type = item_json[@"type"];
  74. NSString* img_url = [item_json valueForKey:@"s"];
  75. NSString* file_name=[img_url lastPathComponent];
  76. NSData* img_data=nil;
  77. if([type isEqualToString:@"video"])
  78. {
  79. img_data = UIImagePNGRepresentation([UIImage imageNamed:@"play"]);
  80. }
  81. else
  82. {
  83. NSLog(@"load img : %@",img_url);
  84. if([item_json[@"is_localfile"] boolValue])
  85. img_data = [NSData dataWithContentsOfFile:img_url];
  86. else
  87. img_data = [FileCache load_cached_img:file_name loadFrom:img_url];
  88. }
  89. if(img_data!=nil)
  90. {
  91. dispatch_async(dispatch_get_main_queue(), ^{
  92. UIImage * img =[UIImage imageWithData:img_data];
  93. [self updateImage:img atIndex:i];
  94. });
  95. }
  96. else
  97. {
  98. NSData* downloadimg_data = nil;
  99. downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  100. NSLog(@"load img");
  101. dispatch_async(dispatch_get_main_queue(), ^{
  102. if(downloadimg_data!=nil)
  103. {
  104. [FileCache cache_img:downloadimg_data filename:file_name saveTo:img_url];
  105. UIImage * img =[UIImage imageWithData:downloadimg_data];
  106. [self updateImage:img atIndex:i];
  107. }
  108. else
  109. {
  110. UIImage * img =[UIImage imageNamed:@"notfound_l"];
  111. [self updateImage:img atIndex:i];
  112. }
  113. }); // gcd
  114. } // else
  115. } // for
  116. });// gcd
  117. [self.bannerCollectionView reloadData];
  118. }
  119. - (void)updateImage:(UIImage *)img atIndex:(NSInteger)index {
  120. if (img == nil) {
  121. NSLog(@"empty image at %ld",index);
  122. return;
  123. }
  124. NSLog(@"loaded image %@ at %ld",img,index);
  125. NSString *key = [NSString stringWithFormat:@"item_%ld",index];
  126. NSMutableDictionary *item_json = [[_contentJson objectForKey:key] mutableCopy];
  127. [item_json setObject:img forKey:@"image"];
  128. [_contentJson setObject:item_json forKey:key];
  129. NSArray<NSIndexPath *> *visibleArr = [self.bannerCollectionView indexPathsForVisibleItems];
  130. for (NSIndexPath *indexPath in visibleArr) {
  131. if (indexPath.row == index) {
  132. // 更新视图
  133. dispatch_async(dispatch_get_main_queue(), ^{
  134. [self.bannerCollectionView reloadItemsAtIndexPaths:@[indexPath]];
  135. });
  136. }
  137. }
  138. }
  139. #pragma mark - Private
  140. - (IBAction)closePlayerBtnClick:(UIButton *)sender {
  141. [self.ytplayerView stopVideo];
  142. self.ytplayerView.hidden=true;
  143. }
  144. #pragma mark - DataSource & Delegate
  145. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  146. int count = [[_contentJson objectForKey:@"count"] intValue];
  147. return count;
  148. }
  149. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  150. CommonEditorBannerItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CommonEditorBannerItemCell" forIndexPath:indexPath];
  151. NSString *key = [NSString stringWithFormat:@"item_%ld",indexPath.row];
  152. NSMutableDictionary *item_json = [[_contentJson objectForKey:key] mutableCopy];
  153. [cell setItem:item_json];
  154. return cell;
  155. }
  156. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  157. return collectionView.bounds.size;
  158. }
  159. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  160. return 0;
  161. }
  162. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  163. if (scrollView == self.bannerCollectionView) {
  164. CGFloat offsetX = scrollView.contentOffset.x;
  165. CGFloat page = offsetX / CGRectGetWidth(scrollView.bounds);
  166. self.pageControlView.currentPage = (int)page;
  167. }
  168. }
  169. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  170. NSDictionary* item= _contentJson[[NSString stringWithFormat:@"item_%ld",indexPath.row]];
  171. if([item[@"type"] isEqualToString:@"video"])
  172. {
  173. // NSString* video_code=item[@"code"];
  174. //
  175. // NSString* video_id=[self.playerView Embed2VID:video_code];
  176. //
  177. // NSDictionary *playerVars = @{
  178. // @"playsinline" : @1,
  179. // @"autoplay" : @(1),
  180. // @"rel":@0,
  181. // @"showinfo": @0,
  182. // @"modestbranding":@0,
  183. // @"enablejsapi":@1
  184. // };
  185. //
  186. // [self.playerView loadWithVideoId:video_id playerVars:playerVars];
  187. // for(UIView *v in self.playerView.subviews)
  188. // {
  189. // if([v isKindOfClass:[UIButton class] ])
  190. // [self.playerView bringSubviewToFront:v];
  191. // }
  192. // if (@available(iOS 9.0, *)) {
  193. // self.playerView.webView.allowsPictureInPictureMediaPlayback=false;
  194. // } else {
  195. // // Fallback on earlier versions
  196. // }
  197. // self.playerView.hidden=false;
  198. NSString* video_code=item[@"code"];
  199. [self.ytplayerView LoadWithVid:video_code];
  200. for(UIView *v in self.ytplayerView.subviews)
  201. {
  202. if([v isKindOfClass:[UIButton class] ])
  203. [self.ytplayerView bringSubviewToFront:v];
  204. }
  205. self.ytplayerView.hidden=false;
  206. return;
  207. }
  208. else
  209. {
  210. // ContentPreviewController *preVC = [[UIStoryboard storyboardWithName:@"PhotoList" bundle:nil] instantiateViewControllerWithIdentifier:@"ContentPreviewController"];
  211. // preVC.content = self.stack_contents;
  212. //
  213. // [preVC setOffset:index];
  214. // UIViewController *superVC= [RAUtils getViewController:self];
  215. // [superVC.navigationController pushViewController:preVC animated:true];
  216. if (self.delegate && [self.delegate respondsToSelector:@selector(commonEditorBannerCell:didClickItemAtIndexPath:)]) {
  217. [self.delegate commonEditorBannerCell:self didClickItemAtIndexPath:indexPath];
  218. }
  219. return;
  220. }
  221. // // 调用展示窗口
  222. // ImageScrollerViewController *imgShow = [[ImageScrollerViewController alloc] initWithSourceData:[self.photos mutableCopy] withIndex:index];
  223. //
  224. // UIViewController *vc= [RAUtils getViewController:self];
  225. // [vc.navigationController pushViewController:imgShow animated:true];
  226. }
  227. @end