// // KBShareImgPopView.m // YouHuiProject // // Created by 小花 on 2018/7/10. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBShareImgPopView.h" #import #import "HYUMShareManager.h" #import @interface KBShareImgPopView (){ UIImage *shareImg; } @property (nonatomic, strong) KBGoodDetailModel *goodModel; @property (nonatomic, strong) UIActivityIndicatorView *indicatorView; @property (nonatomic, strong) KBShareGoodsModel *model; @end @implementation KBShareImgPopView - (instancetype)initWithFrame:(CGRect)frame goodModel:(KBGoodDetailModel *)goodModel{ self = [super initWithFrame:frame]; if (self) { self.goodModel = goodModel; [self initSubViews]; [self loadShareData]; } return self; } - (void)initSubViews { UIView *bg = [[UIView alloc] initWithFrame:self.bounds]; bg.backgroundColor = [UIColor clearColor]; [self addSubview:bg]; UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.bounds]; toolbar.barStyle = UIBarStyleBlack; [bg addSubview:toolbar]; self.shareGoodView = [[KBPopShareGoodView alloc] initWithFrame:CGRectMake(0, Fitsize(30), Fitsize(254), Fitsize(455))]; self.shareGoodView.centerX = self.width/2; self.shareGoodView.backgroundColor = [UIColor whiteColor]; self.shareGoodView.imgSuccBlock = ^{ shareImg = [self.shareGoodView changeToImage]; }; [bg addSubview:self.shareGoodView]; [self.shareGoodView addSubview:self.indicatorView]; [self.indicatorView startAnimating]; NSArray *titles = @[@"分享朋友圈",@"分享给好友",@"复制淘口令",@"保存图片"]; NSArray *icons = @[@"wx_section",@"wx_wechat",@"copy_code",@"save_img"]; CGFloat margin = Fitsize(30); CGFloat width = (self.width-Fitsize(60))/4; CGFloat height = width; for (int i = 0; i < titles.count; i++) { UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(margin+i*width, self.shareGoodView.bottom+Fitsize(20), width, height)]; [button setTitle:titles[i] forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:Fitsize(12)]; [button setImage:[UIImage imageNamed:icons[i]] forState:UIControlStateNormal]; [button setButtonImageTitleStyle:ButtonImageTitleStyleTop padding:10]; button.tag = 1000+i; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [bg addSubview:button]; if (i<2 && ![WXApi isWXAppInstalled]) { button.hidden = YES; } } UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-80, 40, 40)]; [closeBtn setImage:[UIImage imageNamed:@"close_share"] forState:UIControlStateNormal]; closeBtn.centerX = self.width/2; [closeBtn addTarget:self action:@selector(clsoeAction) forControlEvents:UIControlEventTouchUpInside]; [bg addSubview:closeBtn]; } - (void)loadShareData { NSString *url=[NSString stringWithFormat:@"%@/api/v2/adzoneCreate/shareCommonGoodsNew",BaseURL]; NSString *coupon_start_time = [self.goodModel.is_coupon boolValue] ? self.goodModel.start_time : @""; NSString *coupon_end_time = [self.goodModel.is_coupon boolValue] ? self.goodModel.end_time : @""; if (!coupon_start_time) coupon_start_time = @""; if (!coupon_end_time) coupon_end_time = @""; NSDictionary *dic=@{ @"goods_id":self.goodModel.goods_id, @"is_coupon":self.goodModel.is_coupon, @"coupon_price":self.goodModel.coupon_price, @"price":self.goodModel.price, @"discount_price":self.goodModel.discount_price, @"commission_rate":self.goodModel.commission_rate, @"coupon_end_time":coupon_end_time, @"coupon_start_time":coupon_start_time }; [KBHttp post:url params:dic success:^(id json) { KBShareGoodsTempModel *tmpModel=[KBShareGoodsTempModel yy_modelWithJSON:json[@"data"]]; KBShareGoodsModel *model=[KBShareGoodsModel createShareGoodsModelByShareGoodsTempModel:tmpModel]; model.commissionPrice=self.goodModel.commission_price;//佣金价格 model.commission_rate=self.goodModel.commission_rate; model.discount_price=self.goodModel.discount_price; model.userinfo = tmpModel.userinfo; self.model = model; self.shareGoodView.model = model; [self.indicatorView stopAnimating]; } failure:^(NSError *error) { }]; } - (UIActivityIndicatorView *)indicatorView { if (!_indicatorView) { _indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)]; _indicatorView.frame= CGRectMake(0, 0, 50, 50); _indicatorView.color = [UIColor homeRedColor]; _indicatorView.center = CGPointMake(self.shareGoodView.width/2, self.shareGoodView.height/2); _indicatorView.hidesWhenStopped = YES; } return _indicatorView; } - (void)buttonAction:(UIButton *)sender { switch (sender.tag-1000) { case 0: //分享到朋友圈 [self shareImgToWeChat:UMSocialPlatformType_WechatTimeLine]; break; case 1: //分享到微信 [self shareImgToWeChat:UMSocialPlatformType_WechatSession]; break; case 2: //复制淘口令 [self copyCode]; break; case 3: //保存图片 [self saveImage]; break; default: break; } } - (void)shareImgToWeChat:(UMSocialPlatformType)platformType { if (shareImg) { [[HYUMShareManager shareInstance] shareImageWithplatformType:platformType withImg:shareImg]; }else { [MBProgressHUD showError:@"图片生成中"]; } } - (void)copyCode { if (self.model.infoStr.length > 0) { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string =[NSString stringWithFormat:@"%@\n--------\n【在售价】%@元\n【券后价】%@元\n--------\n复制这条信息¥%@¥\n打开【手机淘宝】即可查看",self.model.title,self.model.originalPrice,self.model.ticketAfterPrice,self.model.infoStr]; [SVProgressHUD showSuccessWithStatus:@"复制成功"]; }else { [SVProgressHUD showErrorWithStatus:@"复制失败"]; } } - (void)saveImage { if (shareImg) { __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init]; [lib writeImageToSavedPhotosAlbum:shareImg.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { if (error) { [MBProgressHUD showMessage:@"保存失败"]; }else { [MBProgressHUD showMessage:@"保存成功"]; } lib = nil; }]; }else { [MBProgressHUD showError:@"图片生成中"]; } } - (void)clsoeAction { if (self.closeAction) { self.closeAction(); } } @end