-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// 1.创建滤镜
CIFilter*filter = [CIFilterfilterWithName:@"CIQRCodeGenerator"];
// 2.清空滤镜(还原默认设置,因为滤镜有可能保存上一次的设置)
[filtersetDefaults];
// 3.创建二维码需要保存的数据
NSData*data = [@"IOS开发很牛逼"dataUsingEncoding:NSUTF8StringEncoding];
// 4.设置数据(KVC)
[filtersetValue:dataforKey:@"inputMessage"];
// 5.生成二维码,获取生成后的二维码图片
CIImage*image = [filteroutputImage];
self.QRCImage.image= [selfcreateNonInterpolatedUIImageFormCIImage:imagewithSize:400];
}
/**
*根据CIImage生成指定大小的UIImage
*
*@paramimage CIImage
*@paramsize图片宽度
*/
- (UIImage*)createNonInterpolatedUIImageFormCIImage:(CIImage*)image withSize:(CGFloat) size
{
CGRectextent =CGRectIntegral(image.extent);
CGFloatscale =MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1.创建bitmap;
size_twidth =CGRectGetWidth(extent) * scale;
size_theight =CGRectGetHeight(extent) * scale;
CGColorSpaceRefcs =CGColorSpaceCreateDeviceGray();
CGContextRefbitmapRef =CGBitmapContextCreate(nil, width, height,8,0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext*context = [CIContextcontextWithOptions:nil];
CGImageRefbitmapImage = [contextcreateCGImage:imagefromRect:extent];
CGContextSetInterpolationQuality(bitmapRef,kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 2.保存bitmap到图片
CGImageRefscaledImage =CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return[UIImageimageWithCGImage:scaledImage];
}
网友评论