2
0

CreateBarCodeViewController.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // CreateBarCodeViewController.m
  3. // LBXScanDemo
  4. //
  5. // Created by lbxia on 2017/1/5.
  6. // Copyright © 2017年 lbx. All rights reserved.
  7. //
  8. #import "CreateBarCodeViewController.h"
  9. #import "LBXAlertAction.h"
  10. #import "LBXScanNative.h"
  11. #import "UIImageView+CornerRadius.h"
  12. #import <WFChatClient/WFCChatClient.h>
  13. #import <WFChatUIKit/WFChatUIKit.h>
  14. #import "SDWebImage/SDWebImage.h"
  15. @interface CreateBarCodeViewController ()
  16. @property (nonatomic, strong)UIImageView *logoView;
  17. @property (nonatomic, strong)UILabel *nameLabel;
  18. @property (nonatomic, strong) UIImageView* logoImgView;
  19. @property (nonatomic, strong) UIView *qrView;
  20. @property (nonatomic, strong) UIImageView* qrImgView;
  21. @property (nonatomic, strong)NSString *qrStr;
  22. @property (nonatomic, strong)NSString *qrLogo;
  23. @property (nonatomic, strong)NSString *labelStr;
  24. @property (nonatomic, strong)WFCCUserInfo *userInfo;
  25. @property (nonatomic, strong)WFCCGroupInfo *groupInfo;
  26. @property (nonatomic, strong)UIActivityIndicatorView *indicatorView;
  27. @end
  28. @implementation CreateBarCodeViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"..." style:UIBarButtonItemStyleDone target:self action:@selector(onRightBtn:)];
  32. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  33. if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
  34. self.edgesForExtendedLayout = UIRectEdgeNone;
  35. }
  36. __weak typeof(self) ws = self;
  37. if (self.qrType == QRType_User) {
  38. self.qrStr = [NSString stringWithFormat:@"wildfirechat://user/%@", self.target];
  39. // self.qrStr = [NSString stringWithFormat:@"wildfirechat://user/%@?from=%@", self.target, [WFCCNetworkService sharedInstance].userId];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
  41. self.userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[WFCCNetworkService sharedInstance].userId refresh:NO];
  42. } else if(self.qrType == QRType_Group) {
  43. // self.qrStr = [NSString stringWithFormat:@"wildfirechat://group/%@", self.target];
  44. self.qrStr = [NSString stringWithFormat:@"wildfirechat://group/%@?from=%@", self.target, [WFCCNetworkService sharedInstance].userId];
  45. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onGroupInfoUpdated:) name:kGroupInfoUpdated object:nil];
  46. self.groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:self.target refresh:NO];
  47. } else if(self.qrType == QRType_Conference) {
  48. self.qrStr = self.conferenceUrl;
  49. self.labelStr = self.conferenceTitle;
  50. }
  51. self.qrImgView = [[UIImageView alloc]init];
  52. _qrImgView.bounds = CGRectMake(0, 0, CGRectGetWidth(self.qrView.frame)-12, CGRectGetWidth(self.qrView.frame)-12);
  53. _qrImgView.center = CGPointMake(CGRectGetWidth(self.qrView.frame)/2, CGRectGetHeight(self.qrView.frame)/2+30);
  54. [self.qrView addSubview:_qrImgView];
  55. [self createQR_logo];
  56. }
  57. - (void)saveImage:(UIImage *)image {
  58. UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
  59. UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] init];
  60. indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
  61. indicator.center = self.view.center;
  62. _indicatorView = indicator;
  63. [[UIApplication sharedApplication].keyWindow addSubview:indicator];
  64. [indicator startAnimating];
  65. }
  66. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
  67. {
  68. [_indicatorView removeFromSuperview];
  69. UILabel *label = [[UILabel alloc] init];
  70. label.textColor = [UIColor whiteColor];
  71. label.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f];
  72. label.layer.cornerRadius = 5;
  73. label.clipsToBounds = YES;
  74. label.bounds = CGRectMake(0, 0, 150, 30);
  75. label.center = self.view.center;
  76. label.textAlignment = NSTextAlignmentCenter;
  77. label.font = [UIFont boldSystemFontOfSize:17];
  78. [[UIApplication sharedApplication].keyWindow addSubview:label];
  79. [[UIApplication sharedApplication].keyWindow bringSubviewToFront:label];
  80. if (error) {
  81. label.text = WFCString(@"SaveFailure");
  82. } else {
  83. label.text = WFCString(@"SaveSuccess");
  84. }
  85. [label performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];
  86. }
  87. - (void)onRightBtn:(id)sender {
  88. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  89. UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:WFCString(@"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  90. }];
  91. UIAlertAction *actionSave = [UIAlertAction actionWithTitle:@"保存二维码" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  92. UIGraphicsBeginImageContext(self.qrView.bounds.size);
  93. [self.qrView.layer renderInContext:UIGraphicsGetCurrentContext()];
  94. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  95. UIGraphicsEndImageContext();
  96. [self saveImage:image];
  97. }];
  98. [actionSheet addAction:actionSave];
  99. [actionSheet addAction:actionCancel];
  100. [self presentViewController:actionSheet animated:YES completion:nil];
  101. }
  102. - (void)setUserInfo:(WFCCUserInfo *)userInfo {
  103. _userInfo = userInfo;
  104. self.qrLogo = userInfo.portrait;
  105. if (userInfo.displayName.length) {
  106. self.labelStr = userInfo.displayName;
  107. } else {
  108. self.labelStr = @"用户";
  109. }
  110. }
  111. - (void)setGroupInfo:(WFCCGroupInfo *)groupInfo {
  112. _groupInfo = groupInfo;
  113. if (groupInfo.portrait.length) {
  114. self.qrLogo = groupInfo.portrait;
  115. } else {
  116. NSString *filePath = [WFCCUtilities getGroupGridPortrait:groupInfo.target width:50 generateIfNotExist:YES defaultUserPortrait:^UIImage *(NSString *userId) {
  117. return [WFCUImage imageNamed:@"PersonalChat"];
  118. }];
  119. self.qrLogo = filePath;
  120. }
  121. if (groupInfo.displayName.length) {
  122. self.labelStr = groupInfo.displayName;
  123. } else {
  124. self.labelStr = @"群组";
  125. }
  126. }
  127. - (void)onUserInfoUpdated:(NSNotification *)notification {
  128. NSArray<WFCCUserInfo *> *userInfoList = notification.userInfo[@"userInfoList"];
  129. for (WFCCUserInfo *userInfo in userInfoList) {
  130. if ([self.target isEqualToString:userInfo.userId]) {
  131. self.userInfo = userInfo;
  132. break;
  133. }
  134. }
  135. }
  136. - (void)onGroupInfoUpdated:(NSNotification *)notification {
  137. NSArray<WFCCGroupInfo *> *groupInfoList = notification.userInfo[@"groupInfoList"];
  138. for (WFCCGroupInfo *groupInfo in groupInfoList) {
  139. if ([self.target isEqualToString:groupInfo.target]) {
  140. self.groupInfo = groupInfo;
  141. break;
  142. }
  143. }
  144. }
  145. /*
  146. #define QRType_User 0
  147. #define QRType_Group 1
  148. #define QRType_Channel 2
  149. #define QRType_Chatroom 3
  150. #define QRType_PC_Session 4
  151. #define QRType_Conference 5
  152. */
  153. - (void)setQrLogo:(NSString *)qrLogo {
  154. _qrLogo = qrLogo;
  155. self.logoImgView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 16, 50, 50)];
  156. NSString *defaultPortrait = @"PersonalChat";
  157. if(self.qrType == 1) {
  158. defaultPortrait = @"group_default_portrait";
  159. } else if(self.qrType == 2) {
  160. defaultPortrait = @"channel_default_portrait";
  161. }
  162. [self.logoImgView sd_setImageWithURL:[NSURL URLWithString:self.qrLogo] placeholderImage:[WFCUImage imageNamed:@"group_default_portrait"]];
  163. [self.qrView addSubview:self.logoImgView];
  164. }
  165. - (void)setLabelStr:(NSString *)labelStr {
  166. _labelStr = labelStr;
  167. self.nameLabel.text = labelStr;
  168. }
  169. - (UILabel *)nameLabel {
  170. if (!_nameLabel) {
  171. _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(72, 28, self.qrView.bounds.size.width - 72 - 16, 22)];
  172. [self.qrView addSubview:_nameLabel];
  173. }
  174. return _nameLabel;
  175. }
  176. - (UIView *)qrView {
  177. if (!_qrView) {
  178. UIView *view = [[UIView alloc]initWithFrame:CGRectMake( (CGRectGetWidth(self.view.frame)-CGRectGetWidth(self.view.frame)*5/6)/2, 100, CGRectGetWidth(self.view.frame)*5/6, CGRectGetWidth(self.view.frame)*5/6+60)];
  179. [self.view addSubview:view];
  180. view.backgroundColor = [UIColor whiteColor];
  181. view.layer.shadowOffset = CGSizeMake(0, 2);
  182. view.layer.shadowRadius = 2;
  183. view.layer.shadowColor = [UIColor blackColor].CGColor;
  184. view.layer.shadowOpacity = 0.5;
  185. _qrView = view;
  186. }
  187. return _qrView;
  188. }
  189. - (void)createQR_logo
  190. {
  191. _qrView.hidden = NO;
  192. _qrImgView.image = [LBXScanNative createQRWithString:self.qrStr QRSize:_qrImgView.bounds.size];
  193. }
  194. - (void)showError:(NSString*)str
  195. {
  196. [LBXAlertAction showAlertWithTitle:@"提示" msg:str buttonsStatement:@[@"知道了"] chooseBlock:nil];
  197. }
  198. - (void)dealloc {
  199. [[NSNotificationCenter defaultCenter] removeObserver:self];
  200. }
  201. @end