// // ShareViewController.m // GIGA // // Created by lianxiang on 2018/9/21. // Copyright © 2018年 com.giga.ios. All rights reserved. // #import "ShareViewController.h" #import "MaskTimeShareViewCell.h" #import "MaskShareTimeMidViewCell.h" #import "MaskShareBoootomViewCell.h" #import "MaskTimeShareActionSheet.h" #import "GIGaUserFileHelper.h" #import #import "GiGaFileNanager.h" //#import "WXApiObject.h" #import #import #import "UIImage+ShotImage.h" #import "GiGaBaseAPiRequest.h" #import "UIImageView+WebCache.h" #import "GIGaSharePageViewController.h" #import "LXCustomActionSheet.h" @interface ShareViewController () @property(nonatomic,strong) UITableView *tab; @property(nonatomic,strong) UIImageView *backimageview ; @property(nonatomic,strong) NSArray *descArr; // @property(nonatomic,strong)NSMutableArray *viewControllers; @end @implementation ShareViewController - (void)viewDidLoad { [super viewDidLoad]; [self creatTabUI]; self.descArr = @[@"生活不易,必须美丽",@"长夜漫漫,不如面膜时间",@"敷片面膜,人间值得",@"打开面膜时间,撰写你与美丽的故事",@"人越美丽,责任越大",@"吃得苦中苦,方为人上人",@"该放弃的决不挽留,该留住的决不放手"]; UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; backBtn.frame = CGRectMake(15,20 +PhoneX_TopMargin, 40, 40); [backBtn setImage:[UIImage imageNamed:@"btn_moment_back"] forState:UIControlStateNormal]; [backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:backBtn]; [self creatBtns]; NC_ADD_TARGET_NAME_OBJECT(self, @selector(usershareSuccessNoti), NC_UseShareSuccessNoti, nil); } -(void)dealloc{ NC_REMOVE_NAME(self, NC_UseShareSuccessNoti, nil); } -(void)creatTabUI{ self.automaticallyAdjustsScrollViewInsets = NO; self.tab = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.tab.separatorStyle = UITableViewCellSelectionStyleNone; self.tab.delegate = self; self.tab.dataSource = self; self.tab.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.tab]; UIImageView *backimageview = [[UIImageView alloc] init]; backimageview.contentMode = UIViewContentModeScaleAspectFill; backimageview.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height); backimageview.userInteractionEnabled = YES; //默认背景 拍照 相册 或本地 if (self.localModel) { backimageview.image = [UIImage imageNamed:self.localModel.localImageName]; }else{ backimageview.image = [GIGaUserFileHelper getUserShareImage:kUSERSHAREA_IMAGENAME]; } self.backimageview = backimageview; self.tab.backgroundView = backimageview; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 3; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0) { MaskTimeShareViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MaskTimeShareViewCell"]; if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:@"MaskTimeShareViewCell" owner:self options:nil] lastObject]; } if (self.localModel) { //本地 cell.descLabe.text = self.localModel.desc; if (self.localModel.descColorflag == 1) { cell.descLabe.textColor = [UIColor whiteColor]; }else{ cell.descLabe.textColor = [UIColor blackColor]; } if (self.localModel.doingColorflag == 1) { cell.doinglabel.textColor = [UIColor whiteColor]; }else if (self.localModel.descColorflag == 3){ cell.doinglabel.textColor = GIGARGB(170, 170, 170, 1); }else{ cell.doinglabel.textColor = [UIColor blackColor]; } cell.shareLabe.image = [UIImage imageNamed:self.localModel.shareLabeimageName]; }else{ //默认 cell.descLabe.text = @"现在做些什么呢?"; } return cell; }else if (indexPath.row == 1){ MaskShareTimeMidViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MaskShareTimeMidViewCell"]; if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:@"MaskShareTimeMidViewCell" owner:self options:nil] lastObject]; if (self.model) { cell.timeLabe.text = self.model.minute; CGFloat percent = [self.model.exquisite floatValue]; int perrr = percent * 100; NSString *per = [NSString stringWithFormat:@"%d",perrr]; cell.percetLabe.text = per; cell.totalLabe.text = self.model.totalMaskNum; NSURL *url = [NSURL URLWithString:self.model.headImgUrl]; [cell.userAvtoar sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"nav_circle_avatar"]]; } } return cell; }else if (indexPath.row == 2){ MaskShareBoootomViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MaskShareBoootomViewCell"]; if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:@"MaskShareBoootomViewCell" owner:self options:nil] lastObject]; } return cell; } return nil; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0) { return 295; }else if (indexPath.row ==1){ return 205; } return 158; } -(void)backBtnAction{ [self dismissViewControllerAnimated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)creatBtns{ UIButton *changeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; changeBtn.backgroundColor = GIGARGB(181, 14, 14, 0.6); NSAttributedString *titl = [GiGaHelper stringWithText:@"更换背景" textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTBOLD(18) leterSpace:0]; [changeBtn setAttributedTitle:titl forState:UIControlStateNormal]; [changeBtn addTarget:self action:@selector(changeAction) forControlEvents:UIControlEventTouchUpInside]; changeBtn.layer.masksToBounds = YES; changeBtn.layer.cornerRadius = 4; [self.view addSubview:changeBtn]; [changeBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(120, 40)); make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-45); make.centerX.mas_equalTo(self.view.mas_centerX).offset(-72); }]; UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom]; shareBtn.backgroundColor = GIGARGB(181, 14, 14, 0.6); NSAttributedString *titl2 = [GiGaHelper stringWithText:@"分享" textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTBOLD(18) leterSpace:0]; [shareBtn setAttributedTitle:titl2 forState:UIControlStateNormal]; [shareBtn addTarget:self action:@selector(shareBtnAction) forControlEvents:UIControlEventTouchUpInside]; shareBtn.layer.masksToBounds = YES; shareBtn.layer.cornerRadius = 4; [self.view addSubview:shareBtn]; [shareBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(120, 40)); make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-45); make.centerX.mas_equalTo(self.view.mas_centerX).offset(72); }]; if (self.localModel) { changeBtn.hidden = YES; [shareBtn mas_updateConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.view.mas_centerX); }]; } } //更换背景 -(void)changeAction{ MaskTimeShareActionSheet * sheet = [MaskTimeShareActionSheet showAtView:self]; [sheet show:^(NSUInteger index) { switch (index) { case 0: {//手机 [self photosAlbum]; } break; case 1: { //系统 GIGaSharePageViewController *share = [[GIGaSharePageViewController alloc] initWithControllers:self.viewControllers]; //share.model = self.model; [self presentViewController:share animated:NO completion:nil]; } break; case 2: { //自拍 [self takePhoto]; } break; default: break; } }]; } #pragma mark 相册 -(void)photosAlbum{ UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; //相册 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { //权限 imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:imagePickerController animated:YES completion:nil]; }else{ NSString *errorStr = @"应用相册受限,请在设置中启用"; [self jxt_showAlertWithTitle:@"" message:errorStr appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) { alertMaker.addActionCancelTitle(@"知道了"); //alertMaker.addActionDefaultTitle(@"去开启"); } actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) { }]; } } //拍照 -(void)takePhoto{ UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { //权限判断应用是否有使用相机的权限 NSString *mediaType = AVMediaTypeVideo;//读取媒体类型 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){ NSString *errorStr = @"应用相机权限受限,请在设置中启用"; [self jxt_showAlertWithTitle:@"" message:errorStr appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) { alertMaker.addActionCancelTitle(@"知道了"); //alertMaker.addActionDefaultTitle(@"去开启"); } actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) { }]; return; } // 设置数据源 imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:imagePickerController animated:YES completion:nil]; } } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { GILog(@"info:%@",info); [picker dismissViewControllerAnimated:YES completion:nil] ; UIImage*img=[info objectForKey:UIImagePickerControllerEditedImage]; UIImage *small = [GIGaUserFileHelper zipScaleWithImage:img]; [GIGaUserFileHelper savaShareImange:kUSERSHAREA_IMAGENAME image:small]; [self.backimageview setImage:small]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil]; } -(void)shareBtnAction{ if (![WXApi isWXAppInstalled]) { GIGA_ShowToast(@"未安装微信"); return; } UIImage *image = [UIImage saveLongImage:self.tab]; LXCustomActionSheet *sheet = [[LXCustomActionSheet alloc] initWithTitle:@"分享到微信" optionsArr:@[@"微信好友",@"朋友圈"] cancelTitle:@"取消" selectedBlock:^(NSInteger index) { if (index==0) { //好友 [self shareImagToWX:image scene:WXSceneSession]; }else if (index==1){ // 朋友圈 [self shareImagToWX:image scene:WXSceneTimeline]; } } cancelBlock:^{ GILog(@"取消"); }]; [sheet show]; } -(void)shareImagToWX:(UIImage *)image scene:(int)scene{ WXMediaMessage *msg = [WXMediaMessage message]; UIImage * masllThumb = [GIGaUserFileHelper scaleFromImage:image toSize:CGSizeMake(100, 100)]; msg.title = @"面膜时间"; msg.description= @"极迦面膜"; [msg setThumbImage:masllThumb]; WXImageObject *imageObject = [WXImageObject object]; NSData *imageData = UIImagePNGRepresentation(image); imageObject.imageData = imageData; msg.mediaObject = imageObject; SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; req.message = msg; req.scene = scene; req.bText = NO; [WXApi sendReq:req]; } //分享成功 -(void)usershareSuccessNoti{ GIGA_ShowToast(@"分享成功"); } - (NSMutableArray *)viewControllers { if (!_viewControllers) { _viewControllers = [NSMutableArray array]; NSArray *descArr = @[@"生活不易,必须美丽",@"长夜漫漫,不如面膜时间",@"敷片面膜,人间值得",@"打开面膜时间,撰写你与美丽的故事",@"人越美丽,责任越大",@"吃得苦中苦,方为人上人",@"该放弃的决不挽留,该留住的决不放手"]; NSArray *images = @[@"sharebg_1",@"sharebg_2",@"sharebg_3",@"sharebg_4",@"sharebg_5",@"sharebg_6",@"sharebg_7"]; NSArray *shareimages = @[@"share_oneblack",@"share_one",@"share_one",@"share_one",@"share_one",@"share_one",@"share_one"]; NSArray *desColor = @[@1,@1,@2,@1,@1,@1,@2]; NSArray*doingcolorS = @[@2,@3,@2,@2,@2,@2,@2]; for (int i = 0; i