CIFilter提供了各种各样的滤镜,其中CIQRCodeGenerator可以用来生成二维码。下面通过一个样例演示如何将字符串(链接也是字符串)生成二维码图片,同时支持在二维码中间放置个性化图标。
效果图如下:
效果图如下:
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //不带图片的二维码图片 let imageViewIcon1 = UIImageView(frame: CGRectMake(20, 20, 150, 150)) imageViewIcon1.image = createQRForString("http://www.hangge.com", qrImageName: "") self.view.addSubview(imageViewIcon1) //带图片的二维码图片 let imageViewIcon2 = UIImageView(frame: CGRectMake(200, 20, 150, 150)) imageViewIcon2.image = createQRForString("http://www.hangge.com", qrImageName: "logo") self.view.addSubview(imageViewIcon2) } //创建二维码图片 func createQRForString(qrString: String?, qrImageName: String?) -> UIImage?{ if let sureQRString = qrString { let stringData = sureQRString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) // 创建一个二维码的滤镜 let qrFilter = CIFilter(name: "CIQRCodeGenerator")! qrFilter.setValue(stringData, forKey: "inputMessage") qrFilter.setValue("H", forKey: "inputCorrectionLevel") let qrCIImage = qrFilter.outputImage // 创建一个颜色滤镜,黑白色 let colorFilter = CIFilter(name: "CIFalseColor")! colorFilter.setDefaults() colorFilter.setValue(qrCIImage, forKey: "inputImage") colorFilter.setValue(CIColor(red: 0, green: 0, blue: 0), forKey: "inputColor0") colorFilter.setValue(CIColor(red: 1, green: 1, blue: 1), forKey: "inputColor1") // 返回二维码image let codeImage = UIImage(CIImage: colorFilter.outputImage! .imageByApplyingTransform(CGAffineTransformMakeScale(5, 5))) // 通常,二维码都是定制的,中间都会放想要表达意思的图片 if let iconImage = UIImage(named: qrImageName!) { let rect = CGRectMake(0, 0, codeImage.size.width, codeImage.size.height) UIGraphicsBeginImageContext(rect.size) codeImage.drawInRect(rect) let avatarSize = CGSizeMake(rect.size.width * 0.25, rect.size.height * 0.25) let x = (rect.width - avatarSize.width) * 0.5 let y = (rect.height - avatarSize.height) * 0.5 iconImage.drawInRect(CGRectMake(x, y, avatarSize.width, avatarSize.height)) let resultImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return resultImage } return codeImage } return nil } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2302
- 用户1336
- 访客10968998
每日一句
Qingming Festival invites us to honor ancestors with quiet reflection and respect.
清明节邀请我们以静思与敬意祭奠祖先。
清明节邀请我们以静思与敬意祭奠祖先。
新会员