// // YOUPAILZShareApplicationWindow.m // VQU // // Created by CY on 2021/9/28. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAILZShareApplicationWindow.h" #import @interface YOUPAILZShareApplicationWindow () @property (nonatomic,strong) UIImageView *youpaipbgV; @end @implementation YOUPAILZShareApplicationWindow - (void)viewDidLoad { [super viewDidLoad]; self.baseView.hidden = YES; [self youpaifinitUI]; } - (void)youpaifinitUI{ UIView *bgV = [[UIView alloc] init]; bgV.layer.cornerRadius = ScaleSize(16.0f); bgV.clipsToBounds = YES; bgV.backgroundColor = LCBkgColor; [self.view addSubview:bgV]; // self.youpaipbgV = bgV; [bgV mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.view); make.centerX.equalTo(self.view); make.size.mas_offset(CGSizeMake(ScaleSize(280.0f), ScaleSize(492.0f))); }]; UIImageView *imgV = [[UIImageView alloc] init]; [imgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:self.youpaipmodel.youpaipposterPicUrl]]; [bgV addSubview:imgV]; self.youpaipbgV = imgV; [imgV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.offset(0.0f); make.height.offset(ScaleSize(404.0f)); }]; UIView *qrcodeV = [[UIView alloc] init]; qrcodeV.backgroundColor = [UIColor whiteColor]; [imgV addSubview:qrcodeV]; [qrcodeV mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.offset(-ScaleSize(20.0f)); make.centerX.equalTo(imgV); make.size.mas_offset(CGSizeMake(ScaleSize(70.0f), ScaleSize(70.0f))); }]; UIImageView *qrcodeImgV = [[UIImageView alloc] init]; qrcodeImgV.image = [LCTools createQRCodeImageHaveCenterIMG:self.youpaipmodel.youpaipqCodeContent centerIMG:nil]; [qrcodeV addSubview:qrcodeImgV]; [qrcodeImgV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.offset(5.0f); make.bottom.right.offset(-5.0f); }]; UIView *footerV = [[UIView alloc] init]; footerV.backgroundColor = [UIColor whiteColor]; [bgV addSubview:footerV]; [footerV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.right.offset(0.0f); make.top.equalTo(imgV.mas_bottom).offset(0.0f); }]; NSArray *shareItems = @[ @{@"image":@"vqu_images_share_weixin_timeline",@"title":@"朋友圈"}, @{@"image":@"vqu_images_share_weixin",@"title":@"微信"}, @{@"image":@"vqu_images_share_qq",@"title":@"QQ"}, @{@"image":@"vqu_images_share_save_local",@"title":@"保存到相册"}, ]; NSMutableArray *items = [NSMutableArray array]; for (NSInteger i = 0; i < shareItems.count; i ++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.tag = i; [btn addTarget:self action:@selector(youpaifItemClick:) forControlEvents:UIControlEventTouchUpInside]; [footerV addSubview:btn]; [items addObject:btn]; NSDictionary *dict = shareItems[i]; UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:dict[@"image"]]]; [btn addSubview:imgV]; [imgV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.offset(ScaleSize(8.0f)); make.centerX.equalTo(btn); make.size.mas_offset(CGSizeMake(ScaleSize(37.0f), ScaleSize(37.0f))); }]; UILabel *titleL = [[UILabel alloc] init]; titleL.font = LCFont(ScaleSize(10.0f)); titleL.textColor = HexColorFromRGB(0x333333); titleL.text = dict[@"title"]; [btn addSubview:titleL]; [titleL mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(btn); make.bottom.offset(-ScaleSize(8.0f)); }]; } [items mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0.0f leadSpacing:ScaleSize(15.0f) tailSpacing:ScaleSize(15.0f)]; [items mas_makeConstraints:^(MASConstraintMaker *make) { make.top.offset(ScaleSize(8.0f)); make.bottom.offset(ScaleSize(-8.0f)); }]; } - (void)youpaifItemClick:(UIButton *)sender{ UIImage *img = [self youpaifconvertViewToImage:self.youpaipbgV]; NSInteger index = sender.tag; if (index == 0) { [self shareWithPlatform:UMSocialPlatformType_WechatTimeLine image:img]; }else if (index == 1){ [self shareWithPlatform:UMSocialPlatformType_WechatSession image:img]; }else if (index == 2){ [self shareWithPlatform:UMSocialPlatformType_QQ image:img]; }else if (index == 3){ [self youpaifsaveImage:img]; } } - (void)shareWithPlatform:(UMSocialPlatformType)platformType image:(UIImage *)image{ UMShareImageObject* imageObject = [[UMShareImageObject alloc]init]; [imageObject setShareImage:image]; UMSocialMessageObject *message = [[UMSocialMessageObject alloc] init]; message.shareObject = imageObject; [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:message currentViewController:nil completion:^(id result, NSError *error) { if (error == nil) { [LCHttpHelper requestWithURLString:ShareTask parameters:@{} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code==0) {//成功 [[NSNotificationCenter defaultCenter]postNotificationName:@"yqApplePaySuccess" object:nil userInfo:nil]; } } failure:^(NSError *error) { }]; } }]; } - (void)youpaifsaveImage:(UIImage *)image{ [self dismissViewControllerAnimated:YES completion:^{ [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [PHAssetChangeRequest creationRequestForAssetFromImage:image]; } completionHandler:^(BOOL success, NSError * _Nullable error) { if (success) { [ZCHUDHelper showTitle:@"保存成功"]; }else{ [ZCHUDHelper showTitle:@"保存失败"]; } }]; }]; } //使用该方法不会模糊,根据屏幕密度计算 - (UIImage *)youpaifconvertViewToImage:(UIView *)view { [view.superview layoutIfNeeded]; // view.layer.cornerRadius = 0.0f; UIImage *imageRet = [[UIImage alloc]init]; // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃 if (CGSizeEqualToSize(view.frame.size, CGSizeZero)) { return nil; } //UIGraphicsBeginImageContextWithOptions(区域大小, 是否是非透明的, 屏幕密度); UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, [UIScreen mainScreen].scale); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; imageRet = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imageRet; } @end