1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| class func setupQRCodeImage(_ text: String) -> UIImage { let filter = CIFilter(name: "CIQRCodeGenerator") filter?.setDefaults() filter?.setValue(text.data(using: String.Encoding.utf8), forKey: "inputMessage") if let outputImage = filter?.outputImage { let qrCodeImage = Util.setupHighDefinitionUIImage(outputImage, size: 140) return qrCodeImage } return UIImage() } class func setupHighDefinitionUIImage(_ image: CIImage, size: CGFloat) -> UIImage { let integral: CGRect = image.extent.integral let proportion: CGFloat = min(size/integral.width, size/integral.height) let width = integral.width * proportion let height = integral.height * proportion let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceGray() let bitmapRef = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: 0)! let context = CIContext(options: nil) let bitmapImage: CGImage = context.createCGImage(image, from: integral)! bitmapRef.interpolationQuality = CGInterpolationQuality.none bitmapRef.scaleBy(x: proportion, y: proportion); bitmapRef.draw(bitmapImage, in: integral); let image: CGImage = bitmapRef.makeImage()! return UIImage(cgImage: image) }
|