iOS开发生成二维码图片(附中间带有小图标二维码)
生成二维码图片也是项目中常用到的,二维码的扫描Git上有很多好用的,这里主要说下二维码的生成
1.普通二维码
1.1方法
/** 生成二维码 QRStering:字符串 imageFloat:二维码图片大小 */ +(UIImage*)createQRCodeWithString:(NSString*)QRSteringwithImgSize:(CGFloat)imageFloat;
1.2方法实现
/** 生成二维码 QRStering:字符串 imageFloat:二维码图片大小 */ +(UIImage*)createQRCodeWithString:(NSString*)QRStringwithImgSize:(CGFloat)imageFloat{ CIFilter*filter=[CIFilterfilterWithName:@"XiaoGuiGe"]; [filtersetDefaults]; NSString*getString=QRString; NSData*dataString=[getStringdataUsingEncoding:NSUTF8StringEncoding]; [filtersetValue:dataStringforKey:@"inputMessage"]; ///获取滤镜输出的图像 CIImage*outImage=[filteroutputImage]; UIImage*imageV=[selfimageWithImageSize:imageFloatwithCIIImage:outImage]; //返回二维码图像 returnimageV; }
2.中间带有小图标的二维码
2.1方法
/** 生成二维码(中间有小图片) QRStering:字符串 centerImage:二维码中间的image对象 */ +(UIImage*)createImgQRCodeWithString:(NSString*)QRStringcenterImage:(UIImage*)centerImage;
2.2方法实现
/** 生成二维码(中间有小图片) QRStering:所需字符串 centerImage:二维码中间的image对象 */ +(UIImage*)createImgQRCodeWithString:(NSString*)QRStringcenterImage:(UIImage*)centerImage{ //创建滤镜对象 CIFilter*filter=[CIFilterfilterWithName:@"XiaoGuiGe"]; //恢复滤镜的默认属性 [filtersetDefaults]; //将字符串转换成NSdata NSData*dataString=[QRStringdataUsingEncoding:NSUTF8StringEncoding]; //设置过滤器的输入值,KVC赋值 [filtersetValue:dataStringforKey:@"inputMessage"]; //获得滤镜输出的图像 CIImage*outImage=[filteroutputImage]; //图片小于(27,27),我们需要放大 outImage=[outImageimageByApplyingTransform:CGAffineTransformMakeScale(20,20)]; //将CIImage类型转成UIImage类型 UIImage*startImage=[UIImageimageWithCIImage:outImage]; //开启绘图,获取图形上下文 UIGraphicsBeginImageContext(startImage.size); //把二维码图片画上去(这里是以图形上下文,左上角为(0,0)点 [startImagedrawInRect:CGRectMake(0,0,startImage.size.width,startImage.size.height)]; //再把小图片画上去 CGFloaticon_imageW=200; CGFloaticon_imageH=icon_imageW; CGFloaticon_imageX=(startImage.size.width-icon_imageW)*0.5; CGFloaticon_imageY=(startImage.size.height-icon_imageH)*0.5; [centerImagedrawInRect:CGRectMake(icon_imageX,icon_imageY,icon_imageW,icon_imageH)]; //获取当前画得的这张图片 UIImage*qrImage=UIGraphicsGetImageFromCurrentImageContext(); //关闭图形上下文 UIGraphicsEndImageContext(); //返回二维码图像 returnqrImage; }
附加方法
/**将CIImage转换成UIImage并放大(内部转换使用)*/ +(UIImage*)imageWithImageSize:(CGFloat)sizewithCIIImage:(CIImage*)ciiImage{ CGRectextent=CGRectIntegral(ciiImage.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:ciiImagefromRect:extent]; CGContextSetInterpolationQuality(bitmapRef,kCGInterpolationNone); CGContextScaleCTM(bitmapRef,scale,scale); CGContextDrawImage(bitmapRef,extent,bitmapImage); //2.保存bitmap到图片 CGImageRefscaledImage=CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return[UIImageimageWithCGImage:scaledImage]; }
总结
以上所述是小编给大家介绍的iOS开发生成二维码图片(附中间带有小图标二维码),希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!