123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- //
- // LDShareGoodViewController.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/12/28.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "LDShareGoodViewController.h"
- #import "CCCopyLabel.h"
- #import "LDImageContainerView.h"
- #import "LDShareImgPopView.h"
- #import "LDShareImageCellModel.h"
- #import "LDBottomShareView.h"
- #import "LDShareDesView.h"
- #import "LDPopShareGoodView.h"
- #import "LDShareManager.h"
- #import <AssetsLibrary/AssetsLibrary.h>
- #import <AssetsLibrary/AssetsLibrary.h>
- #import <Photos/Photos.h>
- #import "LDShareTimeLinePopView.h"
- #import "CCAlertShowView.h"
- @interface LDShareGoodViewController ()<BottomShareViewDelegate>
- {
- UIImage *_shareImg;//海报图片
- }
- @property (nonatomic, strong) NSMutableArray *imageArray;
- @property (nonatomic, strong) UIScrollView *scrollView;
- @property (nonatomic, strong) UIView *containerView;
- @property (nonatomic, strong) CCCopyLabel *contentLabel;
- @property (nonatomic, strong) UIView *line;
- @property (nonatomic, strong) LDImageContainerView *imgContainer;
- //@property (nonatomic, strong) UIButton *showCommissionBtn;
- @property (nonatomic, strong) LDBottomShareView *bottomShareView;
- @property (nonatomic, strong) LDShareDesView *shareDesView;
- @property (nonatomic, strong) LDPopShareGoodView *shareGoodView;
- @end
- @implementation LDShareGoodViewController
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- [SVProgressHUD dismiss];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self configNavigationBar];
- [self initSubViews];
- [self requestData];
- }
- - (void)configNavigationBar {
- [self.navigationBar setNavTitle:@"分享商品"];
- self.navigationBar.backgroundColor = [UIColor changeColor];
- self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
- UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
- [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
- [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
- [self.navigationBar setCustomLeftButtons:@[leftBtn]];
- }
- - (void)backAction {
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (void)requestData {
-
- for (NSString *imgUrl in self.goodModel.small_images) {
- LDShareImageCellModel *model = [[LDShareImageCellModel alloc] init];
- model.imgUrl = imgUrl;
- [self.imageArray addObject:model];
- }
-
- LDShareImageCellModel *firstModel = [[LDShareImageCellModel alloc] init];
- firstModel.isSelected = YES;
- [self.imageArray insertObject:firstModel atIndex:0];
- self.shareGoodView = [[LDPopShareGoodView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, Fitsize(254), Fitsize(455))];
- self.shareGoodView.backgroundColor = [UIColor whiteColor];
- [self.view addSubview:self.shareGoodView];
- __weak typeof(self) weakSelf = self;
- self.shareGoodView.imgSuccBlock = ^{
- UIImage *resultImg = [weakSelf.shareGoodView changeToImage];
- _shareImg = resultImg;
- firstModel.shareImg = resultImg;
- [weakSelf.imgContainer reloadWithDataArray:weakSelf.imageArray];
- };
- [self.imgContainer reloadWithDataArray:self.imageArray];
-
- self.shareGoodView.model = self.goodModel;
- }
- - (void)initSubViews {
-
- [self.view addSubview:self.scrollView];
- [self.scrollView addSubview:self.containerView];
- [self.containerView addSubview:self.contentLabel];
- [self.containerView addSubview:self.line];
- [self.containerView addSubview:self.imgContainer];
- // [self.containerView addSubview:self.showCommissionBtn];
- [self.view addSubview:self.shareDesView];
- [self.view addSubview:self.bottomShareView];
-
-
- [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(NavBarHeight);
- make.left.mas_equalTo(0);
- make.width.mas_equalTo(SCREEN_WIDTH);
- make.height.mas_equalTo(SCREEN_HEIGHT-NavBarHeight);
- }];
-
- [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(self.scrollView);
- make.width.equalTo(self.scrollView);
- }];
-
- [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.mas_equalTo(15);
- make.right.mas_equalTo(-15);
- }];
-
- [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(15);
- make.right.mas_equalTo(-15);
- make.height.mas_equalTo(1);
- make.top.mas_equalTo(self.contentLabel.mas_bottom).mas_offset(15);
- }];
-
- UIButton *tipeBtn =[[UIButton alloc]init];
- [tipeBtn setTitle:@"好友扫描二维码后将成为你的潜在好友" forState:UIControlStateNormal];
- [tipeBtn setTitleColor:[UIColor YHColorWithHex:0x393939] forState:UIControlStateNormal];
- [tipeBtn setImage:[UIImage imageNamed:@"share_tip"] forState:UIControlStateNormal];
- tipeBtn.titleLabel.font=[UIFont systemFontOfSize:14];
- [tipeBtn setButtonStyle:WSLButtonStyleImageLeft spacing:5];
- [self.containerView addSubview:tipeBtn];
-
-
- [self.imgContainer mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.mas_equalTo(0);
- make.top.mas_equalTo(self.line.mas_bottom).mas_offset(10);
- make.height.mas_equalTo(150);
- }];
-
- // [self.showCommissionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.left.mas_equalTo(15);
- // make.top.mas_equalTo(self.imgContainer.mas_bottom).mas_offset(20);
- // make.width.mas_equalTo(200);
- // make.height.mas_equalTo(30);
- // }];
-
-
- [self.bottomShareView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(0);
- make.bottom.mas_equalTo(-BottomMargin);
- make.width.mas_equalTo(SCREEN_WIDTH);
- make.height.mas_equalTo(SCREEN_WIDTH/4);
- }];
-
-
- [self.shareDesView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(0);
- make.width.mas_equalTo(SCREEN_WIDTH);
- make.bottom.mas_equalTo(self.bottomShareView.mas_top);
- make.height.mas_equalTo(40);
- }];
-
-
- [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.bottom.mas_equalTo(self.imgContainer.mas_bottom).mas_offset(160);// 这里放最后一个view的底部
- }];
-
- [tipeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(self.imgContainer.mas_bottom).offset(25);
- make.left.mas_equalTo(15);
- }];
- }
- - (void)showSelectAction:(UIButton *)sender {
- sender.selected = !sender.selected;
- if (sender.selected) {
- NSString *text = self.goodModel.tkl_content;
- self.contentLabel.text = text;
- }else {
- NSString *text =self.goodModel.tkl_content;
- self.contentLabel.text = text;
- }
- }
- #pragma mark ----- bottomShareViewDelegate -----
- - (void)BottomShareViewDidSelectedIndex:(NSInteger)index {
- switch (index) {
- case 0:
- //分享到朋友圈
- [self shareImgToWeChat:UMSocialPlatformType_WechatTimeLine];
- [MobClick event:NewGoodDetailCircleShare label:@"朋友圈"];
- break;
- case 1:
- //分享到微信
- [self shareImgToWeChat:UMSocialPlatformType_WechatSession];
- [MobClick event:NewGoodDetailFriendShare label:@"微信好友"];
- break;
- case 2:
- //复制淘口令
- [self copyCode];
- break;
- case 3:
- //保存图片
- [self saveImage:nil];
- break;
- default:
- break;
- }
- }
- /**
- 分享
- */
- - (void)shareImgToWeChat:(UMSocialPlatformType)platformType {
-
- switch (platformType) {
- case UMSocialPlatformType_WechatSession:
- [self shareToWechatSession];
- break;
- case UMSocialPlatformType_WechatTimeLine:
- [self shareToWechatTimeLine];
- break;
- default:
- break;
- }
-
-
-
- }
- /**
- 分享到朋友圈
- */
- - (void)shareToWechatTimeLine {
- //获取选中的图片
- NSMutableArray *saveArray = [NSMutableArray array];
- for (LDShareImageCellModel *model in self.imageArray) {
- if (model.isSelected) {
- [saveArray addObject:model];
- }
- }
- if (saveArray.count == 0) {
- [XHToast showCenterWithText:@"您还没有选择图片"];
- return;
- }
- if (saveArray.count == 1) {
- LDShareImageCellModel *model = saveArray.firstObject;
- if (model.imgUrl) {
- [LDShareManager shareImageWithplatformType:UMSocialPlatformType_WechatTimeLine withImg:model.imgUrl complete:nil];
- }else {
- if (_shareImg) {
- [LDShareManager shareImageWithplatformType:UMSocialPlatformType_WechatTimeLine withImg:_shareImg complete:nil];
- }else {
- [XHToast showCenterWithText:@"图片加载中"];
- }
-
- }
- }else {
-
- LDShareTimeLinePopView *popView = [[LDShareTimeLinePopView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(325), Fitsize(240))];
- CCAlertShowView *showView = [CCAlertShowView showAlertViewWithView:popView backgroundDismissEnable:NO];
- popView.cancelBlock = ^{
- [showView dismiss];
- };
- popView.openBlock = ^{
- [self saveImage:^{
- NSURL *url = [NSURL URLWithString:@"weixin://"];
- [[UIApplication sharedApplication] openURL:url];
- }];
- };
-
-
- }
- }
- /**
- 分享到好友
- */
- - (void)shareToWechatSession {
-
- if (!_shareImg) {
- [XHToast showCenterWithText:@"图片加载中"];
- return;
- }
- if (self.shareResult) {
- self.shareResult();
- }
-
- //获取选中的图片
- NSMutableArray *saveArray = [NSMutableArray array];
- for (LDShareImageCellModel *model in self.imageArray) {
- if (model.isSelected) {
- [saveArray addObject:model];
- }
- }
- if (saveArray.count == 0) {
- [XHToast showCenterWithText:@"您还没有选择图片"];
- return;
- }
- if(saveArray.count == 1)
- {
- LDShareImageCellModel *model = saveArray.firstObject;
- if (model.imgUrl) {
- [LDShareManager shareImageWithplatformType:UMSocialPlatformType_WechatSession withImg:model.imgUrl complete:nil];
- }else {
- if (_shareImg) {
- [LDShareManager shareImageWithplatformType:UMSocialPlatformType_WechatSession withImg:_shareImg complete:nil];
- }else {
- [XHToast showCenterWithText:@"图片加载中"];
- }
-
- }
- }else{
- [LoadingView show];
- NSMutableArray *activityItems = [NSMutableArray array];
- for (LDShareImageCellModel *model in saveArray) {
- if (model.imgUrl) {
- [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.imgUrl] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
- if (image) {
- [activityItems addObject:image];
- if (activityItems.count == saveArray.count) {
- [self showActivityViewController:activityItems];
- }
- }
-
- }];
- }else {
-
- [activityItems addObject:_shareImg];
- if (activityItems.count == saveArray.count) {
- [self showActivityViewController:activityItems];
- }
- }
- }
- }
-
- }
- - (void)showActivityViewController:(NSMutableArray *)activityItems {
-
- NSArray *imgArr = activityItems.mutableCopy;
- UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:imgArr applicationActivities:nil];
- activityVC.modalPresentationStyle = UIModalPresentationFullScreen;
- [self presentViewController:activityVC animated:TRUE completion:^{
- [LoadingView dismiss];
- }];
-
- //回调
- activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
- if (completed) {
-
- }else{
-
- }
- };
- }
- /**
- 复制邀请码
- */
- - (void)copyCode {
- if (self.goodModel.tkl && self.contentLabel.text) {
- UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
- pasteboard.string = self.contentLabel.text;
- [XHToast showCenterWithText:@"复制成功"];
- }else {
- [XHToast showCenterWithText:@"复制失败"];
- }
-
- }
- - (void)saveImage:(void(^)(void))complete {
- if (_shareImg) {
- __block NSInteger count = 0;
- NSMutableArray *saveArray = [NSMutableArray array];
- [self.imageArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- LDShareImageCellModel *model = obj;
- if (model.isSelected) {
- [saveArray addObject:model];
- }
- }];
- if (saveArray.count == 0) {
- [XHToast showCenterWithText:@"您还没有选择图片"];
- return;
- }
-
-
- [SVProgressHUD showProgress:count/saveArray.count status:[NSString stringWithFormat:@"%ld/%lu",(long)count,(unsigned long)saveArray.count]];
- for (int i = 0; i < saveArray.count; i++) {
- LDShareImageCellModel *model = saveArray[i];
- if (model.shareImg) {
- __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
- [lib writeImageToSavedPhotosAlbum:model.shareImg.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
- if (error) {
- UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
- [alert show];
- }else {
- count++;
- [SVProgressHUD showProgress:count/saveArray.count status:[NSString stringWithFormat:@"%ld/%lu",(long)count,(unsigned long)saveArray.count]];
- if (count == saveArray.count) {
- [SVProgressHUD dismiss];
- [XHToast showCenterWithText:@"保存成功"];
- if (complete) {
- complete();
- }
- }
- }
- lib = nil;
- }];
- }else {
- [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.imgUrl] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
- __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
- [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
- if (error) {
- UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
- [alert show];
- }else {
- count++;
- [SVProgressHUD showProgress:count/saveArray.count status:[NSString stringWithFormat:@"%ld/%lu",(long)count,(unsigned long)saveArray.count]];
- if (count == saveArray.count) {
- [SVProgressHUD dismiss];
- [XHToast showCenterWithText:@"保存成功"];
- if (complete) {
- complete();
- }
- }
- }
- lib = nil;
- }];
-
- }];
- }
- }
-
-
- }else {
- [XHToast showCenterWithText:@"图片生成中"];
- }
- }
- #pragma mark -----
- - (CCCopyLabel *)contentLabel {
- if (!_contentLabel) {
- _contentLabel = [[CCCopyLabel alloc] init];
- _contentLabel.font = [UIFont systemFontOfSize:15];
- _contentLabel.numberOfLines = 0;
- NSString *text = self.goodModel.tkl_content;
- _contentLabel.text = text;
- }
- return _contentLabel;
- }
- - (UIView *)line {
- if (!_line) {
- _line = [[UIView alloc] init];
- _line.backgroundColor = [UIColor YHColorWithHex:0xE5E4E6];
- }
- return _line;
- }
- - (UIScrollView *)scrollView {
- if (!_scrollView) {
- _scrollView = [[UIScrollView alloc] init];
- }
- return _scrollView;
- }
- - (UIView *)containerView {
- if (!_containerView) {
- _containerView = [UIView new];
- }
- return _containerView;
- }
- - (LDImageContainerView *)imgContainer {
- if (!_imgContainer) {
- _imgContainer = [LDImageContainerView new];
- }
- return _imgContainer;
- }
- //- (UIButton *)showCommissionBtn {
- // if (!_showCommissionBtn) {
- // _showCommissionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- // [_showCommissionBtn setTitle:@"显示猎豆优选收益" forState:UIControlStateNormal];
- // _showCommissionBtn.titleLabel.font = [UIFont systemFontOfSize:14];
- // [_showCommissionBtn setTitleColor:[UIColor YHColorWithHex:0x393939] forState:UIControlStateNormal];
- // [_showCommissionBtn setImage:[UIImage imageNamed:@"showSel_n"] forState:UIControlStateNormal];
- // [_showCommissionBtn setImage:[UIImage imageNamed:@"showSel_s"] forState:UIControlStateSelected];
- // _showCommissionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
- // [_showCommissionBtn setButtonImageTitleStyle:ButtonImageTitleStyleLeft padding:4];
- // [_showCommissionBtn addTarget:self action:@selector(showSelectAction:) forControlEvents:UIControlEventTouchUpInside];
- // }
- // return _showCommissionBtn;
- //}
- - (NSMutableArray *)imageArray {
- if (!_imageArray) {
- _imageArray = [NSMutableArray array];
- }
- return _imageArray;
- }
- - (LDBottomShareView *)bottomShareView {
- if (!_bottomShareView) {
- _bottomShareView = [[LDBottomShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/4)];
- _bottomShareView.delegate = self;
- }
- return _bottomShareView;
- }
- - (LDShareDesView *)shareDesView {
- if (!_shareDesView) {
- _shareDesView = [[LDShareDesView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) commissPrice:self.goodModel.self_commission];
- }
- return _shareDesView;
- }
- @end
|