iOSS--生成有logo的二维码
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 400, 400)]; [self.view addSubview:_imageview]; [self createLogoImage]; } - (void)createLogoImage { NSArray *filter = [CIFilter filterNamesInCategory:kCICategoryBuiltIn]; NSLog(@"%@", filter); // 二维码过滤器 CIFilter *filterImage = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 将二位码过滤器设置为默认属性 [filterImage setDefaults]; // 将文字转化为二进制 NSData *dataImage = [@"mafeng" dataUsingEncoding:NSUTF8StringEncoding]; // 打印输入的属性 NSLog(@"%@", filterImage.inputKeys); // KVC 赋值 [filterImage setValue:dataImage forKey:@"inputMessage"]; // 取出输出图片 CIImage *outputImage = [filterImage outputImage]; outputImage = [outputImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)]; // 转化图片 UIImage *image = [UIImage imageWithCIImage:outputImage]; // **********为二维码加自定义图片*********** // 开启绘图, 获取图片 上下文<图片大小> UIGraphicsBeginImageContext(image.size); // 将二维码图片画上去 [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; // 将小图片画上去 UIImage *smallImage = [UIImage imageNamed:@"123.jpg"]; [smallImage drawInRect:CGRectMake((image.size.width - 100) / 2, (image.size.width - 100) / 2, 100, 100)]; // 获取最终的图片 UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文 UIGraphicsEndImageContext(); // 显示 self.imageview.image = finalImage; }
效果展示: