话不多说直接上代码
- (void)viewDidLoad {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [self createCoreImage:@"123"];
[self.view addSubview:imageView];
imageView.frame = CGRectMake(200, 200, 100, 100);
}
- (UIImage*)createCoreImage:(NSString*)codeStr
{
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filtersetDefaults];
[filtersetValue:[codeStr dataUsingEncoding:NSUTF8StringEncoding] forKey:@"inputMessage"];
// 获取生成的图片
CIImage*cilmg = filter.outputImage;
CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"];
[colorFiltersetDefaults];
[colorFiltersetValue:cilmgforKey:@"inputImage"];
[colorFiltersetValue:[CIColor colorWithRed:0 green:0 blue:255] forKey:@"inputColor0"];
[colorFiltersetValue:[CIColor colorWithRed:1 green:1 blue:1] forKey:@"inputColor1"];
cilmg = colorFilter.outputImage;
CGAffineTransform scale = CGAffineTransformMakeScale(10, 10);
cilmg = [cilmgimageByApplyingTransform:scale];
// 在中心生成一张图片
UIImage *img = [UIImage imageWithCIImage:cilmg];
// 生成图片
UIGraphicsBeginImageContext(img.size);
//将二维码的图片画入
[imgdrawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
//在中心画入其他图片
UIImage *centerImg = [UIImage imageNamed:@"1024的.png"];
[centerImgdrawInRect:CGRectMake((img.size.width - 50)*0.5, (img.size.height - 50) * 0.5, 50, 50)];
//获取绘制好的图片
UIImage *finalImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnfinalImg;
}