这是我写的用于在swift中生成QRcode的代码.
原文链接:https://www.f2er.com/swift/319213.html//MARK:- generate QR code func generateQRImage(stringQR:NSString,withSizeRate rate:CGFloat) -> UIImage { var filter:CIFilter = CIFilter(name:"CIQRCodeGenerator") filter.setDefaults() var data:NSData = stringQR.dataUsingEncoding(NSUTF8StringEncoding)! filter.setValue(data,forKey: "inputMessage") var outputImg:CIImage = filter.outputImage var context:CIContext = CIContext(options: nil) var cgimg:CGImageRef = context.createCGImage(outputImg,fromRect: outputImg.extent()) var img:UIImage = UIImage(CGImage: cgimg,scale: 1.0,orientation: UIImageOrientation.Up)! var width = img.size.width * rate var height = img.size.height * rate UIGraphicsBeginImageContext(CGSizeMake(width,height)) var cgContxt:CGContextRef = UIGraphicsGetCurrentContext() CGContextSetInterpolationQuality(cgContxt,kCGInterpolationNone) img.drawInRect(CGRectMake(0,width,height)) img = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return img }