heavyrain lee 5 years ago
parent
commit
6ba93ce3a1

+ 2 - 20
wfchat/WildFireChat/AppDelegate.m

@@ -361,26 +361,8 @@ void systemAudioCallback (SystemSoundID soundID, void* clientData) {
 #pragma mark - QrCodeDelegate
 - (void)showQrCodeViewController:(UINavigationController *)navigator type:(int)type target:(NSString *)target {
     CreateBarCodeViewController *vc = [CreateBarCodeViewController new];
-    if (type == QRType_Me) {
-        WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[WFCCNetworkService sharedInstance].userId refresh:NO];
-        vc.str = [NSString stringWithFormat:@"wildfirechat://user/%@", userInfo.userId];
-        vc.logoUrl = userInfo.portrait;
-        if (userInfo.displayName.length) {
-            vc.labelStr = userInfo.displayName;
-        } else {
-            vc.labelStr = @"用户";
-        }
-    } else if(type == QRType_Group) {
-        WFCCGroupInfo *groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:target refresh:NO];
-        vc.str = [NSString stringWithFormat:@"wildfirechat://group/%@", target];
-        vc.logoUrl = groupInfo.portrait;
-        if (groupInfo.name.length) {
-            vc.labelStr = groupInfo.name;
-        } else {
-            vc.labelStr = @"群组";
-        }
-    }
-    
+    vc.qrType = type;
+    vc.target = target;
     [navigator pushViewController:vc animated:YES];
 }
 

+ 2 - 3
wfchat/WildFireChat/LBXScan/CreateBarCodeViewController.h

@@ -9,7 +9,6 @@
 #import <UIKit/UIKit.h>
 
 @interface CreateBarCodeViewController : UIViewController
-@property (nonatomic, strong)NSString *str;
-@property (nonatomic, strong)NSString *logoUrl;
-@property (nonatomic, strong)NSString *labelStr;
+@property (nonatomic, assign)int qrType;
+@property (nonatomic, strong)NSString *target;
 @end

+ 142 - 15
wfchat/WildFireChat/LBXScan/CreateBarCodeViewController.m

@@ -11,6 +11,8 @@
 #import "LBXScanNative.h"
 #import "UIImageView+CornerRadius.h"
 #import <WFChatClient/WFCChatClient.h>
+#import <WFChatUIKit/WFChatUIKit.h>
+
 
 @interface CreateBarCodeViewController ()
 @property (nonatomic, strong)UIImageView *logoView;
@@ -19,37 +21,134 @@
 
 @property (nonatomic, strong) UIView *qrView;
 @property (nonatomic, strong) UIImageView* qrImgView;
+
+@property (nonatomic, strong)NSString *qrStr;
+@property (nonatomic, strong)NSString *qrLogo;
+@property (nonatomic, strong)NSString *labelStr;
+
+@property (nonatomic, strong)WFCCUserInfo *userInfo;
+@property (nonatomic, strong)WFCCGroupInfo *groupInfo;
+
+@property (nonatomic, strong)UIActivityIndicatorView *indicatorView;
 @end
 
 @implementation CreateBarCodeViewController
 
 - (void)viewDidLoad {
     [super viewDidLoad];
-    // Do any additional setup after loading the view.
     
+    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"..." style:UIBarButtonItemStyleDone target:self action:@selector(onRightBtn:)];
+    
+    self.view.backgroundColor = [UIColor colorWithRed:239/255.f green:239/255.f blue:239/255.f alpha:1.0f];
     if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
         self.edgesForExtendedLayout = UIRectEdgeNone;
     }
     
-    self.view.backgroundColor = [UIColor colorWithRed:239/255.f green:239/255.f blue:239/255.f alpha:1.0f];
+    if (self.qrType == QRType_User) {
+        self.qrStr = [NSString stringWithFormat:@"wildfirechat://user/%@", self.target];
+        self.userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[WFCCNetworkService sharedInstance].userId refresh:NO];
+        
+        
+        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:self.target];
+    } else if(self.qrType == QRType_Group) {
+        self.qrStr = [NSString stringWithFormat:@"wildfirechat://group/%@", self.target];
+        
+        self.groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:self.target refresh:NO];
+
+        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onGroupInfoUpdated:) name:kGroupInfoUpdated object:self.target];
+    }
+}
+
+
+- (void)saveImage:(UIImage *)image {
+    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
+    
+    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] init];
+    indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
+    indicator.center = self.view.center;
+    _indicatorView = indicator;
+    [[UIApplication sharedApplication].keyWindow addSubview:indicator];
+    [indicator startAnimating];
+}
+
+- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
+{
+    [_indicatorView removeFromSuperview];
+    
+    UILabel *label = [[UILabel alloc] init];
+    label.textColor = [UIColor whiteColor];
+    label.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f];
+    label.layer.cornerRadius = 5;
+    label.clipsToBounds = YES;
+    label.bounds = CGRectMake(0, 0, 150, 30);
+    label.center = self.view.center;
+    label.textAlignment = NSTextAlignmentCenter;
+    label.font = [UIFont boldSystemFontOfSize:17];
+    [[UIApplication sharedApplication].keyWindow addSubview:label];
+    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:label];
+    if (error) {
+        label.text = @"保存失败";
+    } else {
+        label.text = @"保存成功";
+    }
+    [label performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];
+}
+
+
+- (void)onRightBtn:(id)sender {
+    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
     
+    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
+        
+    }];
+    UIAlertAction *actionSave = [UIAlertAction actionWithTitle:@"保存二维码" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
+        UIGraphicsBeginImageContext(self.qrView.bounds.size);
+        [self.qrView.layer renderInContext:UIGraphicsGetCurrentContext()];
+        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
+        UIGraphicsEndImageContext();
+        [self saveImage:image];
+    }];
     
-    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)];
-    [self.view addSubview:view];
-    view.backgroundColor = [UIColor whiteColor];
-    view.layer.shadowOffset = CGSizeMake(0, 2);
-    view.layer.shadowRadius = 2;
-    view.layer.shadowColor = [UIColor blackColor].CGColor;
-    view.layer.shadowOpacity = 0.5;
-    self.qrView = view;
-    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(72, 15, self.qrView.bounds.size.width - 72 - 16, 22)];
-    self.nameLabel.text = self.labelStr;
-    [self.qrView addSubview:self.nameLabel];
+    [actionSheet addAction:actionSave];
+    [actionSheet addAction:actionCancel];
     
+    [self presentViewController:actionSheet animated:YES completion:nil];
+}
+
+- (void)setUserInfo:(WFCCUserInfo *)userInfo {
+    _userInfo = userInfo;
+    self.qrLogo = userInfo.portrait;
+    if (userInfo.displayName.length) {
+        self.labelStr = userInfo.displayName;
+    } else {
+        self.labelStr = @"用户";
+    }
+}
+
+- (void)setGroupInfo:(WFCCGroupInfo *)groupInfo {
+    _groupInfo = groupInfo;
+    self.qrLogo = groupInfo.portrait;
+    if (groupInfo.name.length) {
+        self.labelStr = groupInfo.name;
+    } else {
+        self.labelStr = @"群组";
+    }
+}
+
+- (void)onUserInfoUpdated:(NSNotification *)notification {
+        self.userInfo = notification.userInfo[@"userInfo"];
+}
+
+- (void)onGroupInfoUpdated:(NSNotification *)notification {
+        self.groupInfo = notification.userInfo[@"groupInfo"];
+}
+
+- (void)setQrLogo:(NSString *)qrLogo {
+    _qrLogo = qrLogo;
     __weak typeof(self)ws = self;
     dispatch_async(dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_DEFAULT), ^{
         CGSize logoSize=CGSizeMake(50, 50);
-        UIImage *logo = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:ws.logoUrl]]];
+        UIImage *logo = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:ws.qrLogo]]];
         dispatch_async(dispatch_get_main_queue(), ^{
             ws.logoImgView = [ws roundCornerWithImage:logo size:logoSize];
             ws.logoImgView.bounds = CGRectMake(0, 0, logoSize.width, logoSize.height);
@@ -59,7 +158,32 @@
     });
 }
 
+- (void)setLabelStr:(NSString *)labelStr {
+    _labelStr = labelStr;
+    self.nameLabel.text = labelStr;
+}
+
+- (UILabel *)nameLabel {
+    if (!_nameLabel) {
+        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(72, 28, self.qrView.bounds.size.width - 72 - 16, 22)];
+        [self.qrView addSubview:_nameLabel];
+    }
+    return _nameLabel;
+}
 
+- (UIView *)qrView {
+    if (!_qrView) {
+        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)];
+        [self.view addSubview:view];
+        view.backgroundColor = [UIColor whiteColor];
+        view.layer.shadowOffset = CGSizeMake(0, 2);
+        view.layer.shadowRadius = 2;
+        view.layer.shadowColor = [UIColor blackColor].CGColor;
+        view.layer.shadowOpacity = 0.5;
+        _qrView = view;
+    }
+    return _qrView;
+}
 - (void)viewDidAppear:(BOOL)animated
 {
     [super viewDidAppear:animated];
@@ -74,7 +198,7 @@
 - (void)createQR_logo
 {
     _qrView.hidden = NO;
-    _qrImgView.image = [LBXScanNative createQRWithString:self.str QRSize:_qrImgView.bounds.size];
+    _qrImgView.image = [LBXScanNative createQRWithString:self.qrStr QRSize:_qrImgView.bounds.size];
 }
 
 - (UIImageView*)roundCornerWithImage:(UIImage*)logoImg size:(CGSize)size
@@ -100,4 +224,7 @@
 }
 
 
+- (void)dealloc {
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
+}
 @end

+ 3 - 3
wfchat/WildFireChat/LBXScan/QQLBXScanViewController.m

@@ -12,6 +12,7 @@
 #import "LBXPermission.h"
 #import "LBXPermissionSetting.h"
 #import <WFChatClient/WFCChatClient.h>
+#import <WFChatUIKit/WFChatUIKit.h>
 
 #define kIs_iPhoneX ([UIScreen mainScreen].bounds.size.height == 812.0f ||[UIScreen mainScreen].bounds.size.height == 896.0f )
 
@@ -281,9 +282,8 @@
 - (void)myQRCode
 {
     CreateBarCodeViewController *vc = [CreateBarCodeViewController new];
-    WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[WFCCNetworkService sharedInstance].userId refresh:NO];
-    vc.str = [NSString stringWithFormat:@"wildfirechat://user/%@", userInfo.userId];
-    vc.logoUrl = userInfo.portrait;
+    vc.qrType = QRType_User;
+    vc.target = [WFCCNetworkService sharedInstance].userId;
     [self.navigationController pushViewController:vc animated:YES];
 }
 

+ 1 - 1
wfuikit/WFChatUIKit/Me/WFCUMyProfileTableViewController.m

@@ -212,7 +212,7 @@
 
 - (void)showMyQrCode {
     if (gQrCodeDelegate) {
-        [gQrCodeDelegate showQrCodeViewController:self.navigationController type:QRType_Me target:nil];
+        [gQrCodeDelegate showQrCodeViewController:self.navigationController type:QRType_User target:nil];
     }
 }
 #pragma mark - UITableViewDelegate

+ 1 - 1
wfuikit/WFChatUIKit/QrCodeHelper.h

@@ -11,7 +11,7 @@
 #define QrCodeHelper_h
 #import <UIKit/UIKit.h>
 
-#define QRType_Me  0
+#define QRType_User  0
 #define QRType_Group 1
 #define QRType_Channel 2
 #define QRType_Chatroom 3