CreateBarCodeViewController.m 8.4 KB

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