123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- //
- // YOUPAILCDynamicContentView.m
- // LiveChat
- //
- // Created by 张灿 on 2018/9/7.
- // Copyright © 2018年 caicai. All rights reserved.
- //
- #import "YOUPAILCDynamicContentView.h"
- #import "YOUPAILCDynamicCell.h"
- #import "YBIBVideoData.h"
- #import <AVKit/AVKit.h>
- #define MarginPadding (15.0f)
- #define IsiPhoneXr ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size) : NO)
- @interface YOUPAILCDynamicContentView()
- @property(nonatomic,strong)UITextView* youpaipcontTextV;
- @property(nonatomic,assign)CGFloat youpaiptxtH;
- @end
- @implementation YOUPAILCDynamicContentView
- - (instancetype)initWithModel:(YOUPAILCDynamicModel*)model{
- if (self = [super init]) {
- self.userInteractionEnabled = YES;
- self.youpaipdyModel = model;
- [self youpaifsetupView];
- }
- return self;
- }
- - (instancetype)initWithFrame:(CGRect)frame model:(YOUPAILCDynamicModel*)model{
- if (self = [super initWithFrame:frame]) {
- self.youpaipdyModel = model;
- [self youpaifsetupView];
- }
- return self;
- }
- - (void)youpaifsetupView{
-
- UITextView* contTextView = [[UITextView alloc]initWithFrame:CGRectMake(MarginPadding, 0, KScreenWidth- MarginPadding - 14.0f,120)];
- //适配暗黑模式 v1.5.6
-
- [contTextView setBackgroundColor:[UIColor clearColor]];
- contTextView.scrollEnabled = NO;
- NSInteger textFont;
- if (IsiPhoneXr) {
- textFont = 15;
- }else{
- textFont = 14;
- }
- // contTextView.backgroundColor = [UIColor redColor];
- // contTextView.userInteractionEnabled = YES;
- // contTextView.numberOfLines = 0;
- // contTextView.lineBreakMode = NSLineBreakByWordWrapping;
- contTextView.textColor = LZ273145Color;
- contTextView.font = [UIFont systemFontOfSize:14];
- contTextView.text = nil;
- contTextView.text = self.youpaipdyModel.youpaipcontent;
- contTextView.editable = NO;
- // contTextView.layer.masksToBounds = YES;
- self.youpaipcontTextV = contTextView;
- [self addSubview:contTextView];
-
- CGSize constraint = CGSizeMake(contTextView.contentSize.width, CGFLOAT_MAX);
-
- // CGSize size = [self.dyModel.content sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
- CGRect rect = [self.youpaipdyModel.youpaipcontent boundingRectWithSize:constraint
- options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
- attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:textFont],
- NSForegroundColorAttributeName : LCTextBlack
- }
- context:nil];
- CGFloat textH = rect.size.height + 18.0;
- self.youpaiptxtH = textH;
- if (textH<=120) {
- contTextView.frame = CGRectMake(MarginPadding, 0, KScreenWidth- MarginPadding- 14.0f, textH);
- self.youpaipcontHeight = textH;
- }else{
- contTextView.frame = CGRectMake(MarginPadding, 0, KScreenWidth- MarginPadding - 14.0f, 120);
- self.youpaipcontHeight = 115;
- self.youpaipcontBtn = [[UIButton alloc]initWithFrame:CGRectMake(MarginPadding, self.youpaipcontHeight, 70, 30)];
- self.youpaipcontBtn.titleLabel.font = LCBoldFont(15);
- [self.youpaipcontBtn setTitle:@"全文" forState:(UIControlStateNormal)];
- [self.youpaipcontBtn setTitleColor:LCYellowColor forState:(UIControlStateNormal)];
- self.youpaipcontBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 25);
- self.youpaipcontBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
- [self.youpaipcontBtn setEnlargeEdge:5];
- [self addSubview:self.youpaipcontBtn];
- self.youpaipcontHeight+=40;
- }
-
- if (self.youpaipdyModel.youpaiptype == 1) {
- [self youpaifsetupVideo];
- }else{
- if (self.youpaipdyModel.youpaipimages.count>0) {
- self.youpaipimgViewArray = [NSMutableArray array];
- [self youpaifsetupImages];
- }
- }
-
- }
- -(void)youpaifsetupVideo{
-
-
- UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(MarginPadding, self.youpaipcontHeight, ScaleSize(213.0f), ScaleSize(284.0f))];
- imgView.contentMode = UIViewContentModeScaleAspectFill;
- imgView.layer.cornerRadius = 10.0;
- imgView.layer.masksToBounds = YES;
- imgView.tag = 0;
- // [imgView sd_setImageWithURL:[NSURL URLWithString:self.youpaipdyModel.youpaipcover_url] placeholderImage:nil];
- YOUPAILCImageModel* imgModel = self.youpaipdyModel.youpaipimages.firstObject;
- NSURL *url = [LCTools getImageUrlWithAddress:imgModel.youpaipurl];
- if(url){
- [self videoImageWithvideoURL:url completion:^(UIImage *image) {
- // 主线程执行:
- dispatch_async(dispatch_get_main_queue(), ^{
- [imgView setImage:image];
- });
-
- }];
- }
-
- [self addSubview:imgView];
- UITapGestureRecognizer* imgTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(youpaifdidClickPlayVideo:)];
- imgView.userInteractionEnabled = YES;
- imgView.multipleTouchEnabled = YES;
- [imgView addGestureRecognizer:imgTap];
- //播放按钮
- UIImageView *playImageView = [UIImageView new];
- [imgView addSubview:playImageView];
- [playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.center.mas_equalTo(0);
- make.size.mas_equalTo(CGSizeMake(52, 52));
- }];
- // playImageView.userInteractionEnabled = YES;
- playImageView.image = [UIImage imageNamed:@"vqu_images_D_dynamic_play"];
-
- playImageView.layer.shadowColor = LCBkgColor.CGColor;//阴影颜色
- playImageView.layer.shadowOffset = CGSizeMake(0, 0);//偏移距离
- playImageView.layer.shadowOpacity = 0.5;//不透明度
- playImageView.layer.shadowRadius = 10.0;//半径
- self.youpaipcontHeight += imgView.frame.size.height;//+10;
- }
- - (void)youpaifsetupImages{
-
- NSArray* imgArray = self.youpaipdyModel.youpaipimages;
- if (imgArray.count==1) {
- YOUPAILCImageModel* imgModel = self.youpaipdyModel.youpaipimages[0];
- CGFloat imgWidth = ScaleSize(213.0f), imgHeight = ScaleSize(284.0f);
- UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(MarginPadding, self.youpaipcontHeight, imgWidth, imgHeight)];
- imgView.contentMode = UIViewContentModeScaleAspectFill;
- imgView.layer.cornerRadius = 10.0;
- imgView.layer.masksToBounds = YES;
- imgView.tag = 0;
- [imgView sd_setImageWithURL:[LCTools getImageUrlWithAddress:imgModel.youpaipurl] placeholderImage:nil];
- [self addSubview:imgView];
- UITapGestureRecognizer* imgTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(youpaifimgTapHandel:)];
- imgView.userInteractionEnabled = YES;
- imgView.multipleTouchEnabled = YES;
- [imgView addGestureRecognizer:imgTap];
-
-
- UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(youpaiflpGR:)];
- //设定最小的长按时间 按不够这个时间不响应手势
- longPressGR.minimumPressDuration=0.7;
- [imgView addGestureRecognizer:longPressGR];
-
- self.youpaipcontHeight += imgView.frame.size.height;//+10;
- [self.youpaipimgViewArray addObject:imgView];
- }else if(imgArray.count==4 || imgArray.count==2){
- CGFloat maxWidth = KScreenWidth - MarginPadding - 14.0f;
- CGFloat width = maxWidth / 2;
- CGFloat wh = width - 3.0f;
- for (int i =0 ; i<imgArray.count; i++) {
-
- NSInteger row = i / 2;
- NSInteger col = i % 2;
- UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(MarginPadding + col * (wh + 3.0f), self.youpaipcontHeight + row * (wh + 3.0f), wh, wh)];
- imgView.contentMode = UIViewContentModeScaleAspectFill;
- imgView.layer.cornerRadius = 8.0;
- imgView.layer.masksToBounds = YES;
- imgView.tag = i;
- YOUPAILCImageModel* imgModel = self.youpaipdyModel.youpaipimages[i];
- [imgView sd_setImageWithURL:[LCTools getImageUrlWithAddress:imgModel.youpaipurl]];
- [self addSubview:imgView];
- UITapGestureRecognizer* imgTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(youpaifimgTapHandel:)];
- imgView.userInteractionEnabled = YES;
- [imgView addGestureRecognizer:imgTap];
-
- UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(youpaiflpGR:)];
- //设定最小的长按时间 按不够这个时间不响应手势
- longPressGR.minimumPressDuration = 0.7;
- [imgView addGestureRecognizer:longPressGR];
-
- [self.youpaipimgViewArray addObject:imgView];
- }
- NSInteger totalRow = (imgArray.count + 1) / 2;
- self.youpaipcontHeight += totalRow * (width + 8.0f);
- }
- else {
- CGFloat maxWidth = KScreenWidth - MarginPadding - 14.0f;
- CGFloat width = maxWidth / 3;
- NSInteger row = (imgArray.count + 2) / 3;
- CGFloat wh = width - 3.0f;
- for (int i =0 ; i<imgArray.count; i++) {
- NSInteger col = i % 3;
- NSInteger row = i / 3;
- UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(MarginPadding + col * (wh + 3.0f), self.youpaipcontHeight + row * (wh + 3.0f), wh, wh)];
- imgView.contentMode = UIViewContentModeScaleAspectFill;
- imgView.layer.cornerRadius = 6.0;
- imgView.layer.masksToBounds = YES;
- imgView.tag = i;
- YOUPAILCImageModel* imgModel = self.youpaipdyModel.youpaipimages[i];
- [imgView sd_setImageWithURL:[[LCTools getImageUrlWithAddress:imgModel.youpaipurl] urlWithImageScale:50]];
- [self addSubview:imgView];
- UITapGestureRecognizer* imgTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(youpaifimgTapHandel:)];
- imgView.userInteractionEnabled = YES;
- [imgView addGestureRecognizer:imgTap];
-
- UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(youpaiflpGR:)];
- //设定最小的长按时间 按不够这个时间不响应手势
- longPressGR.minimumPressDuration = 0.7;
- [imgView addGestureRecognizer:longPressGR];
- [self.youpaipimgViewArray addObject:imgView];
- }
- CGFloat totalH = width * [@(row) floatValue];
- self.youpaipcontHeight += totalH;// + 10.0f;//((KScreenWidth-40)/3.0+5)*(int)ceilf((imgArray.count)/(float)2)+10;
- }
- // else{
- // for (int i =0 ; i<imgArray.count; i++) {
- // UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(MarginPadding+((KScreenWidth-MarginPadding-20-20)/3+10)*(i%3), self.contHeight+((KScreenWidth-MarginPadding-20-20)/3+10)*(i/3), ((KScreenWidth-MarginPadding-20-20)/3), ((KScreenWidth-MarginPadding-20-20)/3))];
- // imgView.contentMode = UIViewContentModeScaleAspectFill;
- // imgView.layer.cornerRadius = 3.0;
- // imgView.layer.masksToBounds = YES;
- // imgView.tag = i;
- // YOUPAILCImageModel* imgModel = self.dyModel.images[i];
- // [imgView sd_setImageWithURL:[LCTools getImageUrlWithAddress:imgModel.url]];
- // [self addSubview:imgView];
- // UITapGestureRecognizer* imgTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(youpaifimgTapHandel:)];
- // imgView.userInteractionEnabled = YES;
- // [imgView addGestureRecognizer:imgTap];
- //
- // UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(youpaiflpGR:)];
- // //设定最小的长按时间 按不够这个时间不响应手势
- // longPressGR.minimumPressDuration=0.7;
- // [imgView addGestureRecognizer:longPressGR];
- //
- // [self.imgViewArray addObject:imgView];
- // }
- // self.contHeight += ((KScreenWidth-40)/3.0+5)*(int)ceilf((imgArray.count)/(float)3)+10;
- // }
-
-
-
- }
- - (void)vqf_youpaifupdateTextHeight{
- CGFloat padValue = self.youpaiptxtH-120;
- if ([self.youpaipcontBtn.titleLabel.text isEqualToString:@"全文"]) {//展开内容
- [UIView animateWithDuration:0.25 animations:^{
- self.youpaipcontTextV.frame = CGRectMake(MarginPadding, 0, KScreenWidth-MarginPadding - 14.0f, 120);
- self.youpaipcontBtn.frame = CGRectMake(MarginPadding, 115, 70, 30);
- for (UIImageView* imgV in self.youpaipimgViewArray) {
- imgV.frame = CGRectMake(imgV.frame.origin.x, imgV.frame.origin.y-padValue, imgV.frame.size.width, imgV.frame.size.height);
- }
- }];
- UIImageView* lastImgV = [self.youpaipimgViewArray lastObject];
- if (lastImgV) {
- self.youpaipcontHeight = CGRectGetMaxY(lastImgV.frame);//+10;
- }else{
- self.youpaipcontHeight = 155;
- }
-
- }else{
- [UIView animateWithDuration:0.25 animations:^{
- // self.contTextV.frame = CGRectMake(15, 0, KScreenWidth-30, self.txtH);
- //self.contBtn.frame = CGRectMake(15, self.txtH-5, 70, 30);
- self.youpaipcontTextV.frame = CGRectMake(MarginPadding, 0, KScreenWidth-MarginPadding - 14.0f, self.youpaiptxtH);
- self.youpaipcontBtn.frame = CGRectMake(MarginPadding, self.youpaiptxtH-5, 70, 30);
- for (UIImageView* imgV in self.youpaipimgViewArray) {
- imgV.frame = CGRectMake(imgV.frame.origin.x, imgV.frame.origin.y+padValue, imgV.frame.size.width, imgV.frame.size.height);
- }
- }];
- UIImageView* lastImgV = [self.youpaipimgViewArray lastObject];
- if (lastImgV) {
- self.youpaipcontHeight = CGRectGetMaxY(lastImgV.frame);//+10;
- }else{
- self.youpaipcontHeight = self.youpaiptxtH+35;
- }
- }
- }
- -(void)youpaifdidClickPlayVideo:(UITapGestureRecognizer *)sender{
-
- UIImageView *imageView = (UIImageView *)sender.view;
- // 视频
- YOUPAILCImageModel* imgModel = self.youpaipdyModel.youpaipimages.firstObject;
- NSURL *url = [LCTools getImageUrlWithAddress:imgModel.youpaipurl];
-
- YBIBVideoData *model = [YBIBVideoData new];
- model.videoURL = url;
- model.thumbImage = imageView.image;
- // model.projectiveView = imageView;
- model.allowSaveToPhotoAlbum = NO; //是否允许保存到相册
- model.autoPlayCount = NSUIntegerMax;
-
- YBImageBrowser *browser = [[YBImageBrowser alloc] init];
- browser.dataSourceArray = @[model];
- browser.shouldHideStatusBar = false;
- browser.currentPage = 0;
- [browser show];
-
-
-
- }
- - (void)youpaifimgTapHandel:(UITapGestureRecognizer*)tap{
- if (tap.state == UIGestureRecognizerStateEnded) {
- UIImageView* imgView = (UIImageView*)tap.view;
- NSArray* imgArray = self.youpaipdyModel.youpaipimages;
- if (imgArray.count>0) {
- NSMutableArray *tempArr = [NSMutableArray array];
- for (int i=0; i<imgArray.count; i++) {
- YOUPAILCImageModel* imgModel = imgArray[i];
- YBIBImageData *model = [YBIBImageData new];
- model.imageURL = [LCTools getImageUrlWithAddress:imgModel.youpaipurl];
- model.projectiveView = self.youpaipimgViewArray[i];
- [tempArr addObject:model];
- }
- YBImageBrowser *browser = [YBImageBrowser new];
- browser.dataSourceArray = tempArr;
- browser.currentPage = imgView.tag;
- [browser show];
- }
- }
- }
- //长按手势
- -(void)youpaiflpGR:(UILongPressGestureRecognizer *)lpGR
- {
- NSString *user_id = [LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id;
- if (![[LCSaveData getServiceIdArr] containsObject:user_id]) {
- return;
- }
- //手势结束
- if (lpGR.state == UIGestureRecognizerStateBegan){
- AudioServicesPlaySystemSound(1519);
- } else if (lpGR.state == UIGestureRecognizerStateEnded){
- NSMutableArray* actionArray = [NSMutableArray array];
- ZCAlertAction* reportAction = [ZCAlertAction actionWithTitle:@"删除图片" andblock:^{//举报
- UIImageView* imgView = (UIImageView*)lpGR.view;
- YOUPAILCImageModel* imgModel = self.youpaipdyModel.youpaipimages[imgView.tag];
- if (imgModel.youpaipimg_id.length == 0) {
- return;
- }
- [LCHttpHelper requestWithURLString:DynamicDeleteLower parameters:@{@"type":@"2",@"info_id":imgModel.youpaipimg_id} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
- NSDictionary* dict = (NSDictionary*)responseObject;
- NSInteger code = [[dict objectForKey:@"code"] integerValue];
- if (code==0) {//成功
- [self.youpaipdyModel.youpaipimages removeObject:imgModel];
-
- if (self.youpaipdelegate && [self.youpaipdelegate respondsToSelector:@selector(youpaifdeleteImage:)]) {
- [self.youpaipdelegate youpaifdeleteImage:self.youpaipdyModel];
- }
-
-
- }
- } failure:^(NSError *error) {}];
-
- }];
-
- [actionArray addObject:reportAction];
- ZCAlertSheetView* alertSheet = [[ZCAlertSheetView alloc]initWithTitle:nil andShowCancelButton:YES andAction:actionArray];
- alertSheet.alertWindow.hidden = NO;
- [alertSheet show];
-
- }
- }
- //获取视频预览图,这里用到了SDImage的缓存图片功能:
- - (void)videoImageWithvideoURL:(NSURL *)videoURL completion:(void (^_Nullable)(UIImage *image))completionBlock{
-
- //先从缓存中找是否有图片
-
- SDImageCache *cache = [SDImageCache sharedImageCache];
- UIImage *memoryImage = [cache imageFromMemoryCacheForKey:videoURL.absoluteString];
- if(memoryImage) {
- completionBlock(memoryImage);
- return;
- }else{
- UIImage*diskImage = [cache imageFromDiskCacheForKey:videoURL.absoluteString];
- if(diskImage) {
- completionBlock(diskImage);
- return;
- }
- }
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- AVURLAsset *asset = [[AVURLAsset alloc]initWithURL:videoURL options:nil];
- NSParameterAssert(asset);
- AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc]initWithAsset:asset];
- assetImageGenerator.appliesPreferredTrackTransform = YES;
- assetImageGenerator.requestedTimeToleranceAfter = kCMTimeZero;//必须设置,否则时间对应不上
- assetImageGenerator.requestedTimeToleranceBefore = kCMTimeZero;//必须设置,否则时间对应不上
- assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
-
- CGImageRef thumbnailImageRef = NULL;
- CFTimeInterval thumbnailImageTime = 1;
- NSError *thumbnailImageGenerationError =nil;
-
- thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMakeWithSeconds(thumbnailImageTime, 10) actualTime:NULL error:&thumbnailImageGenerationError];
-
- if(!thumbnailImageRef)
-
- NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);
-
- UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage:thumbnailImageRef] :nil;
- if(thumbnailImage){
- completionBlock(thumbnailImage);
- dispatch_async(dispatch_get_main_queue(), ^{
- SDImageCache *cache = [SDImageCache sharedImageCache];
-
- // [cache storeImageToMemory:thumbnailImage forKey:videoURL.absoluteString];
- [cache storeImage:thumbnailImage forKey:videoURL.absoluteString completion:^{
-
- }];
- });
- }
-
-
- });
-
- }
- @end
|