CreateBarCodeViewController.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. @interface CreateBarCodeViewController ()
  15. @property (nonatomic, strong)UIImageView *logoView;
  16. @property (nonatomic, strong)UILabel *nameLabel;
  17. @property (nonatomic, strong) UIImageView* logoImgView;
  18. @property (nonatomic, strong) UIView *qrView;
  19. @property (nonatomic, strong) UIImageView* qrImgView;
  20. @property (nonatomic, strong)NSString *qrStr;
  21. @property (nonatomic, strong)NSString *qrLogo;
  22. @property (nonatomic, strong)NSString *labelStr;
  23. @property (nonatomic, strong)WFCCUserInfo *userInfo;
  24. @property (nonatomic, strong)WFCCGroupInfo *groupInfo;
  25. @property (nonatomic, strong)UIActivityIndicatorView *indicatorView;
  26. @end
  27. @implementation CreateBarCodeViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"..." style:UIBarButtonItemStyleDone target:self action:@selector(onRightBtn:)];
  31. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  32. if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
  33. self.edgesForExtendedLayout = UIRectEdgeNone;
  34. }
  35. __weak typeof(self) ws = self;
  36. if (self.qrType == QRType_User) {
  37. self.qrStr = [NSString stringWithFormat:@"wildfirechat://user/%@", self.target];
  38. [[NSNotificationCenter defaultCenter] addObserverForName:kUserInfoUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull notification) {
  39. if ([ws.target isEqualToString:notification.object]) {
  40. ws.userInfo = notification.userInfo[@"userInfo"];
  41. }
  42. }];
  43. self.userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[WFCCNetworkService sharedInstance].userId refresh:NO];
  44. } else if(self.qrType == QRType_Group) {
  45. self.qrStr = [NSString stringWithFormat:@"wildfirechat://group/%@", self.target];
  46. [[NSNotificationCenter defaultCenter] addObserverForName:kGroupInfoUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull notification) {
  47. if ([ws.target isEqualToString:notification.object]) {
  48. ws.groupInfo = notification.userInfo[@"groupInfo"];
  49. }
  50. }];
  51. self.groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:self.target refresh:NO];
  52. }
  53. }
  54. - (void)saveImage:(UIImage *)image {
  55. UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
  56. UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] init];
  57. indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
  58. indicator.center = self.view.center;
  59. _indicatorView = indicator;
  60. [[UIApplication sharedApplication].keyWindow addSubview:indicator];
  61. [indicator startAnimating];
  62. }
  63. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
  64. {
  65. [_indicatorView removeFromSuperview];
  66. UILabel *label = [[UILabel alloc] init];
  67. label.textColor = [UIColor whiteColor];
  68. label.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f];
  69. label.layer.cornerRadius = 5;
  70. label.clipsToBounds = YES;
  71. label.bounds = CGRectMake(0, 0, 150, 30);
  72. label.center = self.view.center;
  73. label.textAlignment = NSTextAlignmentCenter;
  74. label.font = [UIFont boldSystemFontOfSize:17];
  75. [[UIApplication sharedApplication].keyWindow addSubview:label];
  76. [[UIApplication sharedApplication].keyWindow bringSubviewToFront:label];
  77. if (error) {
  78. label.text = @"保存失败";
  79. } else {
  80. label.text = @"保存成功";
  81. }
  82. [label performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];
  83. }
  84. - (void)onRightBtn:(id)sender {
  85. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  86. UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  87. }];
  88. UIAlertAction *actionSave = [UIAlertAction actionWithTitle:@"保存二维码" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  89. UIGraphicsBeginImageContext(self.qrView.bounds.size);
  90. [self.qrView.layer renderInContext:UIGraphicsGetCurrentContext()];
  91. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  92. UIGraphicsEndImageContext();
  93. [self saveImage:image];
  94. }];
  95. [actionSheet addAction:actionSave];
  96. [actionSheet addAction:actionCancel];
  97. [self presentViewController:actionSheet animated:YES completion:nil];
  98. }
  99. - (void)setUserInfo:(WFCCUserInfo *)userInfo {
  100. _userInfo = userInfo;
  101. self.qrLogo = userInfo.portrait;
  102. if (userInfo.displayName.length) {
  103. self.labelStr = userInfo.displayName;
  104. } else {
  105. self.labelStr = @"用户";
  106. }
  107. }
  108. - (void)setGroupInfo:(WFCCGroupInfo *)groupInfo {
  109. _groupInfo = groupInfo;
  110. self.qrLogo = groupInfo.portrait;
  111. if (groupInfo.name.length) {
  112. self.labelStr = groupInfo.name;
  113. } else {
  114. self.labelStr = @"群组";
  115. }
  116. }
  117. - (void)onUserInfoUpdated:(NSNotification *)notification {
  118. self.userInfo = notification.userInfo[@"userInfo"];
  119. }
  120. - (void)onGroupInfoUpdated:(NSNotification *)notification {
  121. self.groupInfo = notification.userInfo[@"groupInfo"];
  122. }
  123. - (void)setQrLogo:(NSString *)qrLogo {
  124. _qrLogo = qrLogo;
  125. __weak typeof(self)ws = self;
  126. dispatch_async(dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_DEFAULT), ^{
  127. CGSize logoSize=CGSizeMake(50, 50);
  128. UIImage *logo = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:ws.qrLogo]]];
  129. dispatch_async(dispatch_get_main_queue(), ^{
  130. ws.logoImgView = [ws roundCornerWithImage:logo size:logoSize];
  131. ws.logoImgView.bounds = CGRectMake(0, 0, logoSize.width, logoSize.height);
  132. ws.logoImgView.center = CGPointMake(40, 40);
  133. [ws.qrView addSubview:ws.logoImgView];
  134. });
  135. });
  136. }
  137. - (void)setLabelStr:(NSString *)labelStr {
  138. _labelStr = labelStr;
  139. self.nameLabel.text = labelStr;
  140. }
  141. - (UILabel *)nameLabel {
  142. if (!_nameLabel) {
  143. _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(72, 28, self.qrView.bounds.size.width - 72 - 16, 22)];
  144. [self.qrView addSubview:_nameLabel];
  145. }
  146. return _nameLabel;
  147. }
  148. - (UIView *)qrView {
  149. if (!_qrView) {
  150. 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)];
  151. [self.view addSubview:view];
  152. view.backgroundColor = [UIColor whiteColor];
  153. view.layer.shadowOffset = CGSizeMake(0, 2);
  154. view.layer.shadowRadius = 2;
  155. view.layer.shadowColor = [UIColor blackColor].CGColor;
  156. view.layer.shadowOpacity = 0.5;
  157. _qrView = view;
  158. }
  159. return _qrView;
  160. }
  161. - (void)viewDidAppear:(BOOL)animated
  162. {
  163. [super viewDidAppear:animated];
  164. self.qrImgView = [[UIImageView alloc]init];
  165. _qrImgView.bounds = CGRectMake(0, 0, CGRectGetWidth(self.qrView.frame)-12, CGRectGetWidth(self.qrView.frame)-12);
  166. _qrImgView.center = CGPointMake(CGRectGetWidth(self.qrView.frame)/2, CGRectGetHeight(self.qrView.frame)/2+30);
  167. [self.qrView addSubview:_qrImgView];
  168. [self createQR_logo];
  169. }
  170. - (void)createQR_logo
  171. {
  172. _qrView.hidden = NO;
  173. _qrImgView.image = [LBXScanNative createQRWithString:self.qrStr QRSize:_qrImgView.bounds.size];
  174. }
  175. - (UIImageView*)roundCornerWithImage:(UIImage*)logoImg size:(CGSize)size
  176. {
  177. //logo圆角
  178. UIImageView *backImage = [[UIImageView alloc] initWithCornerRadiusAdvance:6.0f rectCornerType:UIRectCornerAllCorners];
  179. backImage.frame = CGRectMake(0, 0, size.width, size.height);
  180. backImage.backgroundColor = [UIColor whiteColor];
  181. UIImageView *logImage = [[UIImageView alloc] initWithCornerRadiusAdvance:6.0f rectCornerType:UIRectCornerAllCorners];
  182. logImage.image =logoImg;
  183. CGFloat diff =2;
  184. logImage.frame = CGRectMake(diff, diff, size.width - 2 * diff, size.height - 2 * diff);
  185. [backImage addSubview:logImage];
  186. return backImage;
  187. }
  188. - (void)showError:(NSString*)str
  189. {
  190. [LBXAlertAction showAlertWithTitle:@"提示" msg:str buttonsStatement:@[@"知道了"] chooseBlock:nil];
  191. }
  192. - (void)dealloc {
  193. [[NSNotificationCenter defaultCenter] removeObserver:self];
  194. }
  195. @end