CNLiveMyQrCodeView.m
7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// CNLiveMyQrCodeView.m
// CNLiveQrCodeModule
//
// Created by 153993236@qq.com on 11/12/2019.
// Copyright © 2019 153993236@qq.com. All rights reserved.
//
#import "CNLiveMyQrCodeView.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
#import "QMUIKit.h"
#import "SGQRCode.h"//二维码库
@interface CNLiveMyQrCodeView ()
@property (nonatomic, strong) UIImageView *header;
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *address;
@property (nonatomic, strong) UIImageView *qrCode;
@property (nonatomic, strong) UILabel *friends;
@end
@implementation CNLiveMyQrCodeView
static const NSInteger margin = 20;
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
self.backgroundColor = [UIColor whiteColor];
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 10;
// 生成二维码
[self generateQRCode:@"gen"];
[self createSubViews];
}
return self;
}
- (void)dealloc {
NSLog(@"XWMyQrCodeView -- dealloc");
}
- (void)createSubViews {
[self addSubview:self.header];
[self addSubview:self.name];
[self addSubview:self.address];
[self addSubview:self.qrCode];
[self addSubview:self.friends];
[_header mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).with.offset(margin);
make.top.equalTo(self.mas_top).with.offset(margin);
make.width.height.offset(60);
}];
[_name mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.header.mas_top);
make.left.equalTo(self.header.mas_right).with.offset(margin/2.0);
make.right.equalTo(self.mas_right).with.offset(-margin);
make.height.offset(30);
}];
[_address mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.name.mas_bottom);
make.left.equalTo(self.name.mas_left);
make.right.equalTo(self.name.mas_right);
make.height.equalTo(self.name.mas_height);
}];
[_qrCode mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.mas_centerX).with.offset(0);
make.top.equalTo(self.header.mas_bottom).with.offset(margin);
make.width.height.equalTo(self.mas_width).with.offset(-4*margin);
}];
[_friends mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left);
make.right.equalTo(self.mas_right);
make.bottom.equalTo(self.mas_bottom).with.offset(-margin);
}];
}
// 重置二维码
- (void)generateQRCode:(NSString *)type{
// NSDictionary *params = @{
// @"appId":@"AppId",
// @"sid":@"uid",
// @"type":type
// };
// __weak typeof(self) weakSelf = self;
// [GetViewData POST:XWGenOrResetCodeUrl params:params success:^(NSString * _Nullable code, id _Nullable dataObj) {
// if (dataObj) {
// __strong typeof(weakSelf) strongSelf = weakSelf;
// if(!strongSelf) return ;
// NSDictionary *dic = @{@"content":[XWCryptoDES encryptUseDES:[self dictionaryToJsonString:@{@"name":@"张晓文",@"id":@"1243124"}] key:AppDESKey],@"type":@"user"};
// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
// NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// UIImage *image = [UIImage xw_getImageName:@"qr_code_icon" bundle:@"XWMyModule.bundle/XWMyModule" targetClass:[XWMyQrCodeView class]];
// strongSelf.qrCode.image = [SGQRCodeObtain generateQRCodeWithData:jsonStr size:180.0 logoImage:image ratio:0.2];
// }else{
// [XWAlertView alertErrorViewWithTextMessage:@"生成失败" view:XW_AppKeyWindow];
//
// }
// } failure:^(NSError * _Nullable error) {
// [XWAlertView alertErrorViewWithTextMessage:@"生成失败" view:XW_AppKeyWindow];
//
// }];
// NSDictionary *dic = @{@"content":[XWCryptoDES encryptUseDES:[self dictionaryToJsonString:@{@"name":@"张晓文",@"id":@"1243124"}] key:AppDESKey],@"type":@"user"};
// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
// NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// UIImage *image = [self xw_getImageName:@"qr_code_icon" bundleName:@"XWMyModule.bundle/XWMyModule" targetClass:[XWMyQrCodeView class]];
// self.qrCode.image = [SGQRCodeObtain generateQRCodeWithData:jsonStr size:180.0 logoImage:image ratio:0.2];
//
}
// 字典转json字符串
- (NSString *)dictionaryToJsonString:(NSDictionary *)dic {
NSError *parseError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
// 保存图片
- (UIImage *)getImage{
return _qrCode.image;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
#pragma mark - 懒加载
- (UIImageView *)header {
if (!_header) {
_header = [[UIImageView alloc] init];
_header.backgroundColor = [UIColor redColor];
_header.layer.masksToBounds = YES;
_header.layer.cornerRadius = 5;
UIImage *xw_image = [self xw_getImageName:@"default_avatar_f" bundleName:@"CNLiveQrCodeModule" targetClass:[CNLiveMyQrCodeView class]];
[_header sd_setImageWithURL:[NSURL URLWithString:@"User_Head"] placeholderImage:xw_image];
}
return _header;
}
- (UILabel *)name {
if (!_name) {
_name = [[UILabel alloc] init];
_name.numberOfLines = 1;
_name.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
_name.font = [UIFont systemFontOfSize:16];
_name.text = @"User_Name";
}
return _name;
}
- (UILabel *)address {
if (!_address) {
_address = [[UILabel alloc] init];
_address.numberOfLines = 1;
_address.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
_address.font = [UIFont systemFontOfSize:14];
_address.text = @"User_Address";
}
return _address;
}
- (UIImageView *)qrCode {
if (!_qrCode) {
_qrCode = [[UIImageView alloc] init];
}
return _qrCode;
}
- (UILabel *)friends {
if (!_friends) {
_friends = [[UILabel alloc] init];
_friends.numberOfLines = 1;
_friends.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
_friends.font = [UIFont systemFontOfSize:16];
_friends.textAlignment = NSTextAlignmentCenter;
_friends.text = @"扫描二维码,加我好友";
}
return _friends;
}
/**
* 创建图片
*
* @param imageName 图片名字
* @param bundleName 图片所在的bundle名字
* @param targetClass 类和bundle的同级目录
* @return 返回UIImage
*/
- (UIImage *)xw_getImageName:(NSString *)imageName bundleName:(NSString *)bundleName targetClass:(Class)targetClass{
NSBundle *bundle = [NSBundle bundleForClass:targetClass];
NSURL *url = [bundle URLForResource:bundleName withExtension:@"bundle"];
NSBundle *targetBundle = [NSBundle bundleWithURL:url];
UIImage *image = [UIImage imageNamed:imageName inBundle:targetBundle compatibleWithTraitCollection:nil];
return image?image:[UIImage imageNamed:imageName inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
}
@end