[iOS]分享一段用系统API生产二维码的代码
2016-02-23 11:04
423 查看
@interface NSString (LyQR) /** * 根据字符串生产二维码图片 【注意:如果要放大显示,请将UIImageView对象的layer.magnificationFilter = kCAFilterNearest; 修改放大算法的选择,保证图片不会模糊】 * * @param foregroundColor 二维码图片的颜色【nil是黑色】 * @param backgroundColor 二维码图片的背景颜色【nil是白色】 * * @return 二维码图片 */ - (UIImage *)ly_QRImageWithForegroundColor:(UIColor *)foregroundColor backgroundColor:(UIColor *)backgroundColor; - (UIImage *)ly_QRImage; @end @implementation NSString (LyQR) - (UIImage *)ly_QRImage; { return [self ly_QRImageWithForegroundColor:nil backgroundColor:nil]; } - (UIImage *)ly_QRImageWithForegroundColor:(UIColor *)foregroundColor backgroundColor:(UIColor *)backgroundColor; { CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; [qrFilter setValue:[self dataUsingEncoding:NSUTF8StringEncoding] forKey:@"inputMessage"]; [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"]; CIImage *ciImage = qrFilter.outputImage; if (foregroundColor != nil || backgroundColor != nil) { CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"]; [colorFilter setValue:ciImage forKey:@"inputImage"]; if (foregroundColor != nil) { [colorFilter setValue: [CIColor colorWithCGColor:foregroundColor.CGColor] forKey:@"inputColor0"]; } if (backgroundColor != nil) { [colorFilter setValue: [CIColor colorWithCGColor:backgroundColor.CGColor] forKey:@"inputColor1"]; } ciImage = [colorFilter outputImage]; } // 生产图片 CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:ciImage fromRect:ciImage.extent]; UIGraphicsBeginImageContext(ciImage.extent.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetInterpolationQuality(context, kCGInterpolationNone); CGContextScaleCTM(context, 1.0, -1.0); // 翻转,否则是生产的二维码是个倒的 CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRelease(cgImage); return image; } @end
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 批处理制作二维码生成器
- jquery插件qrcode在线生成二维码
- JavaScript生成二维码图片小结
- php制作中间带自己定义图片二维码的方法
- zbar解码二维码和条形码示例
- php使用qr生成二维码的示例分享
- php实现扫描二维码根据浏览器类型访问不同下载地址
- PHP微信开发之二维码生成类
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- 浅析iOS应用开发中线程间的通信与线程安全问题