| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- //
- // CommonEditorBannerCell.m
- // TestPhoto
- //
- // Created by Jack on 2017/11/30.
- // Copyright © 2017年 Jack. All rights reserved.
- //
- #import "CommonEditorBannerCell.h"
- #import "CommonEditorBannerItemCell.h"
- #import "RAYTPlayer.h"
- #import "FileCache.h"
- @interface CommonEditorBannerCell () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
- {
- NSMutableDictionary *_contentJson;
- }
- @property (weak, nonatomic) IBOutlet RAYTPlayer *ytplayerView;
- @property (strong, nonatomic) IBOutlet UIPageControl *pageControlView;
- @property (strong, nonatomic) IBOutlet UICollectionView *bannerCollectionView;
- @property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;
- @end
- @implementation CommonEditorBannerCell
- - (void)awakeFromNib {
- [super awakeFromNib];
- // Initialization code
- [self configBanner];
- }
- - (instancetype)initWithCoder:(NSCoder *)aDecoder {
- if (self = [super initWithCoder:aDecoder]) {
- [self configBanner];
- }
- return self;
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- [self configBanner];
- }
- return self;
- }
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
- [self configBanner];
- }
- return self;
- }
- #pragma mark -
- - (void)configBanner {
- if (self.bannerCollectionView) {
- [self.bannerCollectionView registerNib:[UINib nibWithNibName:@"Phone_Banner_Item_Cell" bundle:nil] forCellWithReuseIdentifier:@"CommonEditorBannerItemCell"];
- self.flowLayout = (UICollectionViewFlowLayout *)self.bannerCollectionView.collectionViewLayout;
- }
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- [self.flowLayout invalidateLayout];
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- #pragma mark - Interface
- - (NSDictionary *)content {
- return _contentJson;
- }
- - (void)setContent:(NSDictionary *)content {
- _contentJson = [content mutableCopy];
-
- int count = [[_contentJson objectForKey:@"count"] intValue];
-
- self.pageControlView.numberOfPages = count;
- self.pageControlView.currentPage = 0;
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- for(int i=0;i<count;i++)
- {
-
- NSDictionary* item_json=[_contentJson objectForKey:[NSString stringWithFormat:@"item_%d",i]];
-
- NSString* type = item_json[@"type"];
-
- NSString* img_url = [item_json valueForKey:@"s"];
-
- NSString* file_name=[img_url lastPathComponent];
- NSData* img_data=nil;
-
- if([type isEqualToString:@"video"])
- {
- img_data = UIImagePNGRepresentation([UIImage imageNamed:@"play"]);
- }
- else
- {
- NSLog(@"load img : %@",img_url);
- if([item_json[@"is_localfile"] boolValue])
- img_data = [NSData dataWithContentsOfFile:img_url];
- else
- img_data = [FileCache load_cached_img:file_name loadFrom:img_url];
- }
-
- if(img_data!=nil)
- {
- dispatch_async(dispatch_get_main_queue(), ^{
- UIImage * img =[UIImage imageWithData:img_data];
- [self updateImage:img atIndex:i];
- });
- }
- else
- {
- NSData* downloadimg_data = nil;
-
- downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
- NSLog(@"load img");
- dispatch_async(dispatch_get_main_queue(), ^{
-
- if(downloadimg_data!=nil)
- {
-
- [FileCache cache_img:downloadimg_data filename:file_name saveTo:img_url];
-
- UIImage * img =[UIImage imageWithData:downloadimg_data];
- [self updateImage:img atIndex:i];
- }
- else
- {
- UIImage * img =[UIImage imageNamed:@"notfound_l"];
- [self updateImage:img atIndex:i];
- }
-
- }); // gcd
-
- } // else
- } // for
- });// gcd
-
- [self.bannerCollectionView reloadData];
- }
- - (void)updateImage:(UIImage *)img atIndex:(NSInteger)index {
- if (img == nil) {
- NSLog(@"empty image at %ld",index);
- return;
- }
- NSLog(@"loaded image %@ at %ld",img,index);
-
- NSString *key = [NSString stringWithFormat:@"item_%ld",index];
- NSMutableDictionary *item_json = [[_contentJson objectForKey:key] mutableCopy];
- [item_json setObject:img forKey:@"image"];
- [_contentJson setObject:item_json forKey:key];
-
- NSArray<NSIndexPath *> *visibleArr = [self.bannerCollectionView indexPathsForVisibleItems];
- for (NSIndexPath *indexPath in visibleArr) {
- if (indexPath.row == index) {
- // 更新视图
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.bannerCollectionView reloadItemsAtIndexPaths:@[indexPath]];
- });
- }
- }
- }
- #pragma mark - Private
- - (IBAction)closePlayerBtnClick:(UIButton *)sender {
- [self.ytplayerView stopVideo];
-
- self.ytplayerView.hidden=true;
- }
- #pragma mark - DataSource & Delegate
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- int count = [[_contentJson objectForKey:@"count"] intValue];
- return count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
-
- CommonEditorBannerItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CommonEditorBannerItemCell" forIndexPath:indexPath];
-
- NSString *key = [NSString stringWithFormat:@"item_%ld",indexPath.row];
- NSMutableDictionary *item_json = [[_contentJson objectForKey:key] mutableCopy];
-
- [cell setItem:item_json];
-
- return cell;
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- return collectionView.bounds.size;
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
- return 0;
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- if (scrollView == self.bannerCollectionView) {
-
- CGFloat offsetX = scrollView.contentOffset.x;
- CGFloat page = offsetX / CGRectGetWidth(scrollView.bounds);
- self.pageControlView.currentPage = (int)page;
- }
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
-
-
-
- NSDictionary* item= _contentJson[[NSString stringWithFormat:@"item_%ld",indexPath.row]];
-
- if([item[@"type"] isEqualToString:@"video"])
- {
-
- // NSString* video_code=item[@"code"];
- //
- // NSString* video_id=[self.playerView Embed2VID:video_code];
- //
- // NSDictionary *playerVars = @{
- // @"playsinline" : @1,
- // @"autoplay" : @(1),
- // @"rel":@0,
- // @"showinfo": @0,
- // @"modestbranding":@0,
- // @"enablejsapi":@1
- // };
- //
- // [self.playerView loadWithVideoId:video_id playerVars:playerVars];
- // for(UIView *v in self.playerView.subviews)
- // {
- // if([v isKindOfClass:[UIButton class] ])
- // [self.playerView bringSubviewToFront:v];
- // }
- // if (@available(iOS 9.0, *)) {
- // self.playerView.webView.allowsPictureInPictureMediaPlayback=false;
- // } else {
- // // Fallback on earlier versions
- // }
- // self.playerView.hidden=false;
-
- NSString* video_code=item[@"code"];
- [self.ytplayerView LoadWithVid:video_code];
- for(UIView *v in self.ytplayerView.subviews)
- {
- if([v isKindOfClass:[UIButton class] ])
- [self.ytplayerView bringSubviewToFront:v];
- }
-
- self.ytplayerView.hidden=false;
-
- return;
- }
- else
- {
- // ContentPreviewController *preVC = [[UIStoryboard storyboardWithName:@"PhotoList" bundle:nil] instantiateViewControllerWithIdentifier:@"ContentPreviewController"];
- // preVC.content = self.stack_contents;
- //
- // [preVC setOffset:index];
- // UIViewController *superVC= [RAUtils getViewController:self];
- // [superVC.navigationController pushViewController:preVC animated:true];
-
-
- if (self.delegate && [self.delegate respondsToSelector:@selector(commonEditorBannerCell:didClickItemAtIndexPath:)]) {
- [self.delegate commonEditorBannerCell:self didClickItemAtIndexPath:indexPath];
- }
-
- return;
- }
-
- // // 调用展示窗口
- // ImageScrollerViewController *imgShow = [[ImageScrollerViewController alloc] initWithSourceData:[self.photos mutableCopy] withIndex:index];
- //
- // UIViewController *vc= [RAUtils getViewController:self];
- // [vc.navigationController pushViewController:imgShow animated:true];
-
- }
- @end
|