CommonEditorBannerCell.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. }
  15. @property (weak, nonatomic) IBOutlet RAYTPlayer *ytplayerView;
  16. @property (strong, nonatomic) IBOutlet UIPageControl *pageControlView;
  17. @property (strong, nonatomic) IBOutlet UICollectionView *bannerCollectionView;
  18. @property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;
  19. @property (nonatomic,strong) NSMutableDictionary *contentJson;
  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 self.contentJson;
  63. }
  64. - (void)setContent:(NSDictionary *)content {
  65. _contentJson = [content mutableCopy];
  66. int count = [[self.contentJson objectForKey:@"count"] intValue];
  67. self.pageControlView.numberOfPages = count;
  68. self.pageControlView.currentPage = 0;
  69. __weak typeof(self) weakself = self;
  70. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  71. for(int i=0;i<count;i++)
  72. {
  73. NSDictionary* item_json=[weakself.contentJson objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  74. NSString* type = item_json[@"type"];
  75. NSString* img_url = [item_json valueForKey:@"s"];
  76. NSString* file_name=[img_url lastPathComponent];
  77. NSData* img_data=nil;
  78. if([type isEqualToString:@"video"])
  79. {
  80. img_data = UIImagePNGRepresentation([UIImage imageNamed:@"play"]);
  81. }
  82. else
  83. {
  84. NSLog(@"load img : %@",img_url);
  85. if([item_json[@"is_localfile"] boolValue])
  86. img_data = [NSData dataWithContentsOfFile:img_url];
  87. else
  88. img_data = [FileCache load_cached_img:file_name loadFrom:img_url];
  89. }
  90. if(img_data!=nil)
  91. {
  92. dispatch_async(dispatch_get_main_queue(), ^{
  93. UIImage * img =[UIImage imageWithData:img_data];
  94. [weakself updateImage:img atIndex:i];
  95. });
  96. }
  97. else
  98. {
  99. NSData* downloadimg_data = nil;
  100. downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  101. NSLog(@"load img");
  102. dispatch_async(dispatch_get_main_queue(), ^{
  103. if(downloadimg_data!=nil)
  104. {
  105. [FileCache cache_img:downloadimg_data filename:file_name saveTo:img_url];
  106. UIImage * img =[UIImage imageWithData:downloadimg_data];
  107. [weakself updateImage:img atIndex:i];
  108. }
  109. else
  110. {
  111. UIImage * img =[UIImage imageNamed:@"notfound_l"];
  112. [weakself updateImage:img atIndex:i];
  113. }
  114. }); // gcd
  115. } // else
  116. } // for
  117. });// gcd
  118. [self.bannerCollectionView reloadData];
  119. }
  120. - (void)updateImage:(UIImage *)img atIndex:(NSInteger)index {
  121. if (img == nil) {
  122. NSLog(@"empty image at %ld",index);
  123. return;
  124. }
  125. NSLog(@"loaded image %@ at %ld",img,index);
  126. NSString *key = [NSString stringWithFormat:@"item_%ld",index];
  127. NSMutableDictionary *item_json = [[_contentJson objectForKey:key] mutableCopy];
  128. [item_json setObject:img forKey:@"image"];
  129. [_contentJson setObject:item_json forKey:key];
  130. NSArray<NSIndexPath *> *visibleArr = [self.bannerCollectionView indexPathsForVisibleItems];
  131. for (NSIndexPath *indexPath in visibleArr) {
  132. if (indexPath.row == index) {
  133. // 更新视图
  134. dispatch_async(dispatch_get_main_queue(), ^{
  135. [self.bannerCollectionView reloadItemsAtIndexPaths:@[indexPath]];
  136. });
  137. }
  138. }
  139. }
  140. #pragma mark - Private
  141. - (IBAction)closePlayerBtnClick:(UIButton *)sender {
  142. [self.ytplayerView stopVideo];
  143. self.ytplayerView.hidden=true;
  144. }
  145. #pragma mark - DataSource & Delegate
  146. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  147. int count = [[_contentJson objectForKey:@"count"] intValue];
  148. return count;
  149. }
  150. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  151. CommonEditorBannerItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CommonEditorBannerItemCell" forIndexPath:indexPath];
  152. NSString *key = [NSString stringWithFormat:@"item_%ld",indexPath.row];
  153. NSMutableDictionary *item_json = [[_contentJson objectForKey:key] mutableCopy];
  154. [cell setItem:item_json];
  155. return cell;
  156. }
  157. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  158. return collectionView.bounds.size;
  159. }
  160. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  161. return 0;
  162. }
  163. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  164. if (scrollView == self.bannerCollectionView) {
  165. CGFloat offsetX = scrollView.contentOffset.x;
  166. CGFloat page = offsetX / CGRectGetWidth(scrollView.bounds);
  167. self.pageControlView.currentPage = (int)page;
  168. }
  169. }
  170. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  171. NSDictionary* item= _contentJson[[NSString stringWithFormat:@"item_%ld",indexPath.row]];
  172. if([item[@"type"] isEqualToString:@"video"])
  173. {
  174. // NSString* video_code=item[@"code"];
  175. //
  176. // NSString* video_id=[self.playerView Embed2VID:video_code];
  177. //
  178. // NSDictionary *playerVars = @{
  179. // @"playsinline" : @1,
  180. // @"autoplay" : @(1),
  181. // @"rel":@0,
  182. // @"showinfo": @0,
  183. // @"modestbranding":@0,
  184. // @"enablejsapi":@1
  185. // };
  186. //
  187. // [self.playerView loadWithVideoId:video_id playerVars:playerVars];
  188. // for(UIView *v in self.playerView.subviews)
  189. // {
  190. // if([v isKindOfClass:[UIButton class] ])
  191. // [self.playerView bringSubviewToFront:v];
  192. // }
  193. // if (@available(iOS 9.0, *)) {
  194. // self.playerView.webView.allowsPictureInPictureMediaPlayback=false;
  195. // } else {
  196. // // Fallback on earlier versions
  197. // }
  198. // self.playerView.hidden=false;
  199. NSString* video_code=item[@"code"];
  200. [self.ytplayerView LoadWithVid:video_code];
  201. for(UIView *v in self.ytplayerView.subviews)
  202. {
  203. if([v isKindOfClass:[UIButton class] ])
  204. [self.ytplayerView bringSubviewToFront:v];
  205. }
  206. self.ytplayerView.hidden=false;
  207. return;
  208. }
  209. else
  210. {
  211. // ContentPreviewController *preVC = [[UIStoryboard storyboardWithName:@"PhotoList" bundle:nil] instantiateViewControllerWithIdentifier:@"ContentPreviewController"];
  212. // preVC.content = self.stack_contents;
  213. //
  214. // [preVC setOffset:index];
  215. // UIViewController *superVC= [RAUtils getViewController:self];
  216. // [superVC.navigationController pushViewController:preVC animated:true];
  217. if (self.delegate && [self.delegate respondsToSelector:@selector(commonEditorBannerCell:didClickItemAtIndexPath:)]) {
  218. [self.delegate commonEditorBannerCell:self didClickItemAtIndexPath:indexPath];
  219. }
  220. return;
  221. }
  222. // // 调用展示窗口
  223. // ImageScrollerViewController *imgShow = [[ImageScrollerViewController alloc] initWithSourceData:[self.photos mutableCopy] withIndex:index];
  224. //
  225. // UIViewController *vc= [RAUtils getViewController:self];
  226. // [vc.navigationController pushViewController:imgShow animated:true];
  227. }
  228. @end