BusinessCardVC.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // BusinessCardVC.m
  3. // YBVideo
  4. //
  5. // Created by ybRRR on 2021/2/25.
  6. // Copyright © 2021 cat. All rights reserved.
  7. //
  8. #import "BusinessCardVC.h"
  9. #import "YBShareView.h"
  10. @interface BusinessCardVC ()
  11. {
  12. UIImageView *backImg;
  13. UIImageView *qrImgView;
  14. UILabel *nameLb;//昵称
  15. UILabel *idLb; //id
  16. UILabel *codeLb;//邀请码
  17. NSString *hrefStr;
  18. UIImage *shareImage;
  19. }
  20. @end
  21. @implementation BusinessCardVC
  22. -(void)requestCodeData{
  23. NSDictionary *dic = @{@"uid":[Config getOwnID],@"token":[Config getOwnToken]};
  24. [YBNetworking postWithUrl:@"Agent.getCode" Dic:dic Suc:^(int code, id info, NSString *msg) {
  25. NSDictionary *infos = [info firstObject];
  26. codeLb.text =minstr([infos valueForKey:@"code"]);
  27. hrefStr = minstr([info valueForKey:@"href"]);
  28. [qrImgView sd_setImageWithURL:[NSURL URLWithString:minstr([infos valueForKey:@"qr"])]];
  29. } Fail:^(id fail) {
  30. }];
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.titleL.text = YZMsg(@"我的名片");
  35. self.rightBtn.hidden = NO;
  36. [self.rightBtn setImage:[UIImage imageNamed:@"code_share"] forState:0];
  37. [self creatUI];
  38. [self requestCodeData];
  39. }
  40. -(void)clickNaviRightBtn{
  41. shareImage = [self getImage:backImg];
  42. //分享
  43. NSData *imageData = UIImagePNGRepresentation(shareImage);
  44. NSDictionary *shareDic = @{@"href":hrefStr,@"shareImage":imageData,@"shareTitle":YZMsg(@"扫描二维码,加我好友")};
  45. [YBShareView showShareWithType:RKShareType_card parameter:shareDic commplete:^(int codeEvent, NSString *nums) {
  46. }];
  47. }
  48. -(void)creatUI{
  49. backImg = [[UIImageView alloc]init];
  50. backImg.frame = CGRectMake(16, 64+statusbarHeight+20, _window_width-32, _window_height*0.6);
  51. backImg.image = [UIImage imageNamed:@"card_bg"];
  52. backImg.backgroundColor = Normal_BackColor;
  53. [self.view addSubview:backImg];
  54. qrImgView = [[UIImageView alloc]init];
  55. qrImgView.frame = CGRectMake((backImg.width-backImg.width *0.35)/2, backImg.height/2/2-(backImg.width *0.35/2), backImg.width *0.35, backImg.width *0.35);
  56. qrImgView.backgroundColor = [UIColor lightGrayColor];
  57. [backImg addSubview:qrImgView];
  58. UIImageView *lineImg = [[UIImageView alloc]init];
  59. lineImg.frame = CGRectMake(backImg.width *0.2, backImg.height *0.6, backImg.width *0.6, 5);
  60. lineImg.image = [UIImage imageNamed:@"card_line"];
  61. [backImg addSubview:lineImg];
  62. idLb = [[UILabel alloc]init];
  63. idLb.frame = CGRectMake(0, lineImg.top-30, backImg.width, 20);
  64. idLb.font = [UIFont systemFontOfSize:14];
  65. idLb.text = [NSString stringWithFormat:@"ID:%@",[Config getOwnID]];
  66. idLb.textColor = [UIColor blackColor];
  67. idLb.textAlignment = NSTextAlignmentCenter;
  68. [backImg addSubview:idLb];
  69. nameLb =[[UILabel alloc]init];
  70. nameLb.frame = CGRectMake(0, idLb.top-30, backImg.width, 20);
  71. nameLb.font = [UIFont boldSystemFontOfSize:16];
  72. nameLb.text = [Config getOwnNicename];
  73. nameLb.textColor = [UIColor blackColor];
  74. nameLb.textAlignment = NSTextAlignmentCenter;
  75. [backImg addSubview:nameLb];
  76. UILabel *codeTitle = [[UILabel alloc]init];
  77. codeTitle.frame = CGRectMake(0, lineImg.bottom+10, backImg.width, 20);
  78. codeTitle.font = [UIFont systemFontOfSize:14];
  79. codeTitle.text = YZMsg(@"邀请码");
  80. codeTitle.textColor = [UIColor blackColor];
  81. codeTitle.textAlignment = NSTextAlignmentCenter;
  82. [backImg addSubview:codeTitle];
  83. codeLb = [[UILabel alloc]init];
  84. codeLb.frame = CGRectMake(0, codeTitle.bottom+5, backImg.width, 20);
  85. codeLb.font = [UIFont boldSystemFontOfSize:16];
  86. codeLb.text = @"CRHDKSL";
  87. codeLb.textColor = [UIColor blackColor];
  88. codeLb.textAlignment = NSTextAlignmentCenter;
  89. [backImg addSubview:codeLb];
  90. UILabel *tips = [[UILabel alloc]init];
  91. tips.frame = CGRectMake(0, codeLb.bottom+10, backImg.width, 20);
  92. tips.font = [UIFont systemFontOfSize:14];
  93. tips.text = YZMsg(@"扫描二维码,加我好友");
  94. tips.textColor = [UIColor blackColor];
  95. tips.textAlignment = NSTextAlignmentCenter;
  96. [backImg addSubview:tips];
  97. UIView *iconView = [[UIView alloc]init];
  98. [backImg addSubview:iconView];
  99. [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
  100. make.centerX.equalTo(backImg.mas_centerX);
  101. make.top.equalTo(tips.mas_bottom).offset(20);
  102. make.height.mas_equalTo(26);
  103. }];
  104. UIImageView *iconImg = [[UIImageView alloc]init];
  105. iconImg.image = [PublicObj getAppIcon];
  106. iconImg.layer.cornerRadius = 5;
  107. iconImg.layer.masksToBounds = YES;
  108. [iconView addSubview:iconImg];
  109. [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
  110. make.width.height.mas_equalTo(25);
  111. make.left.equalTo(iconView.mas_left);
  112. make.top.equalTo(iconView.mas_top);
  113. }];
  114. UILabel *appName = [[UILabel alloc]init];
  115. appName.font = [UIFont boldSystemFontOfSize:16];
  116. appName.textColor = [UIColor blackColor];
  117. appName.text =[PublicObj getAppName];
  118. [iconView addSubview:appName];
  119. [appName mas_makeConstraints:^(MASConstraintMaker *make) {
  120. make.left.equalTo(iconImg.mas_right).offset(5);
  121. make.centerY.equalTo(iconImg.mas_centerY);
  122. make.right.equalTo(iconView.mas_right);
  123. }];
  124. UIButton *saveBtn = [UIButton buttonWithType:0];
  125. saveBtn.frame = CGRectMake(_window_width/2-40, _window_height *0.85, 70, 70);
  126. [saveBtn setImage:[UIImage imageNamed:@"card_dowloadBtn"] forState:0];
  127. [saveBtn setTitle:YZMsg(@"保存到相册") forState:0];
  128. saveBtn.titleLabel.font = [UIFont systemFontOfSize:13];
  129. saveBtn = [PublicObj setUpImgDownText:saveBtn];
  130. saveBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
  131. [saveBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
  132. [self.view addSubview:saveBtn];
  133. }
  134. -(void)saveBtnClick{
  135. shareImage = [self getImage:backImg];
  136. [[PHPhotoLibrary sharedPhotoLibrary]performChanges:^{
  137. [PHAssetChangeRequest creationRequestForAssetFromImage:shareImage];
  138. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  139. if (error) {
  140. NSLog(@"%@",@"保存失败");
  141. dispatch_async(dispatch_get_main_queue(), ^{
  142. [MBProgressHUD showError:YZMsg(@"保存失败")];
  143. });
  144. } else {
  145. NSLog(@"%@",@"保存成功");
  146. dispatch_async(dispatch_get_main_queue(), ^{
  147. [MBProgressHUD showError:YZMsg(@"保存成功")];
  148. });
  149. }
  150. }];
  151. }
  152. - (UIImage *)getImage:(UIView *)shareView
  153. {
  154. UIGraphicsBeginImageContextWithOptions(CGSizeMake(shareView.frame.size.width,shareView.frame.size.height ), NO, 0.0); //currentView 当前的view  创建一个基于位图的图形上下文并指定大小为
  155. [shareView.layer renderInContext:UIGraphicsGetCurrentContext()];
  156. // renderInContext呈现接受者及其子范围到指定的上下文
  157. UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片
  158. UIGraphicsEndImageContext();//移除栈顶的基于当前位图的图形上下文
  159. // UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);//然后将该图片保存到图片图
  160. return viewImage;
  161. }
  162. @end