iOS 在二维码中间加图片,改变二维码的颜色

项目中实现在对二维码的处理(记录)

//coreImage框架初始二维码


- (void)CreateCoreImage:(NSString *)codeStr{

//1.生成coreImage框架中的滤镜来生产二维码

CIFilter *Filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

[Filter setDefaults];

[Filter setValue:[codeStr dataUsingEncoding:NSUTF8StringEncoding] forKey:@"inputMessage"];

//4.获取生成的图片

CIImage *ciImg=Filter.outputImage;

//放大ciImg,默认生产的图片很小

//5.设置二维码的前景色和背景颜色

CIFilter *colorFilter=[CIFilter filterWithName:@"CIFalseColor"];

//5.1设置默认值

[colorFilter setDefaults];

[colorFilter setValue:ciImg forKey:@"inputImage"];

[colorFilter setValue:[CIColor colorWithRed:0 green:0 blue:255] forKey:@"inputColor0"];

[colorFilter setValue:[CIColor colorWithRed:1 green:1 blue:1] forKey:@"inputColor1"];

//5.3获取生存的图片

ciImg=colorFilter.outputImage;

CGAffineTransform scale=CGAffineTransformMakeScale(10, 10);

ciImg=[ciImg imageByApplyingTransform:scale];

//    self.imgView.image=[UIImage imageWithCIImage:ciImg];

//6.在中心增加一张图片

UIImage *img=[UIImage imageWithCIImage:ciImg];

//7.生存图片

//7.1开启图形上下文

UIGraphicsBeginImageContext(img.size);

//7.2将二维码的图片画入

//BSXPCMessage received error for message: Connection interrupted  why??

//    [img drawInRect:CGRectMake(10, 10, img.size.width-20, img.size.height-20)];

[img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];

//7.3在中心划入其他图片

UIImage *centerImg=[UIImage imageNamed:@"shrad"];

CGFloat centerW=70;

CGFloat centerH=70;

CGFloat centerX=(img.size.width-70)*0.5;

CGFloat centerY=(img.size.height -70)*0.5;

[centerImg drawInRect:CGRectMake(centerX, centerY, centerW, centerH)];

//7.4获取绘制好的图片

UIImage *finalImg=UIGraphicsGetImageFromCurrentImageContext();

//7.5关闭图像上下文

UIGraphicsEndImageContext();

//设置图片

_img.image = finalImg;

_img.userInteractionEnabled = YES;

//长按手势识别器

UILongPressGestureRecognizer *pressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];

[_img addGestureRecognizer:pressGesture];

}


//保存图片


-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture

{

if(gesture.state == UIGestureRecognizerStateBegan)

{

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action)

{

}];

UIAlertAction *camera = [UIAlertAction actionWithTitle:@"保存图片到手机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)

{

UIImageWriteToSavedPhotosAlbum(_img.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);

}];

[alert addAction:cancle];

[alert addAction:camera];

[self presentViewController:alert animated:YES completion:nil];

}

}


你可能感兴趣的:(iOS 在二维码中间加图片,改变二维码的颜色)