// // QRcodeViewController.m // watch // // Created by xTT on 2017/7/17. // Copyright © 2017年 xTT. All rights reserved. // #import "QRcodeViewController.h" #import "Circle.h" @interface QRcodeViewController () @end @implementation QRcodeViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // self.codeBG.layer.borderColor = [UIColor colorWithWhite:0.5 alpha:0.5].CGColor; // self.codeBG.layer.borderWidth = 1; self.zx_navTitle = @"二维码"; self.label_name.text = cUser.cDevice.name; self.deviceImage.layer.cornerRadius = CGRectGetWidth(self.deviceImage.frame)/2; self.deviceImage.layer.masksToBounds = YES; self.imeiLabel.text = [NSString stringWithFormat:@"IMEI/MEID:%@",cUser.cDevice.imei]; UIImage *defaImage ; if([cUser.cDevice.sex isEqualToString:@"girl"]){ defaImage = [UIImage imageNamed:@"icon_girl_head_portrait_1"]; }else{ defaImage = [UIImage imageNamed:@"icon_boy_head_portrait_1"]; } [self.deviceImage sd_setImageWithURL:[NSURL URLWithString:cUser.cDevice.avator] placeholderImage:defaImage]; WEAKSELF Circle *circle = [cUser getCircleWithImei:cUser.cDevice.imei]; [circle getCircleInviteTokenSuccess:^(id responseObject){ NSString *codeStr = [weakSelf getUriWithDic:@{ // @"inviteToken":responseObject[@"inviteToken"], @"groupid":circle.groupid, @"imei":cUser.cDevice.imei} Url:responseObject[@"baseURL"]]; [weakSelf setImageViewWithCodeStr:codeStr]; // weakSelf.label_InviteToken.text = [NSString stringWithFormat:@"邀请码:%@",responseObject[@"inviteToken"]]; } failure:^{ [weakSelf setImageViewWithCodeStr:cUser.cDevice.imei]; }]; } - (void)setImageViewWithCodeStr:(NSString *)codeStr{ CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; [filter setDefaults]; NSData *data = [codeStr dataUsingEncoding:NSUTF8StringEncoding]; [filter setValue:data forKeyPath:@"inputMessage"]; // 4.获取输出的二维码 CIImage *outputImage = [filter outputImage]; self.imageView_Code.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:_imageView_Code.frame.size.width]; } - (NSString *)getUriWithDic:(NSDictionary *)dic Url:(NSString *)Url{ if (!Url) { Url = @""; } NSMutableArray *arr = [NSMutableArray array]; [dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { [arr addObject:[NSString stringWithFormat:@"%@=%@",key,obj]]; }]; NSRange range = [Url rangeOfString:@"?"]; if (range.location == NSNotFound) { return [NSString stringWithFormat:@"%@?%@",Url,[arr componentsJoinedByString:@"&"]]; }else{ return [NSString stringWithFormat:@"%@&%@",Url,[arr componentsJoinedByString:@"&"]]; } } //高清二维码 - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size { CGRect extent = CGRectIntegral(image.extent); //设置比例 CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); // 创建bitmap(位图); size_t width = CGRectGetWidth(extent) * scale; size_t height = CGRectGetHeight(extent) * scale; CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray(); CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone); CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef bitmapImage = [context createCGImage:image fromRect:extent]; CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone); CGContextScaleCTM(bitmapRef, scale, scale); CGContextDrawImage(bitmapRef, extent, bitmapImage); // 保存bitmap到图片 CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end