Swift 生成二维码方法 (高清处理生成二维码)

    ///--生成二维码
        /// CIFilter
        let filter = CIFilter.init(name: "CIQRCodeGenerator")
        filter?.setDefaults()
        /// Add Data
        //链接转换
        let data = "https://XXXXX.com".data(using: .utf8)
        filter?.setValue(data, forKeyPath: "inputMessage")
        /// Out Put
        let outputImage = filter?.outputImage
        /// Show QRCode
        shareUI.logoImage.image = createUIImageFromCIImage(image: outputImage!, size: 50)
// 高清处理 -- 生成二维码
   static func createUIImageFromCIImage(image: CIImage, size: CGFloat) -> UIImage {
        let extent = image.extent.integral
        let scale = min(size / extent.width, size / extent.height)
            
        /// Create bitmap
        let width: size_t = size_t(extent.width * scale)
        let height: size_t = size_t(extent.height * scale)
        let cs: CGColorSpace = CGColorSpaceCreateDeviceGray()
        let bitmap: CGContext = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: cs, bitmapInfo: 1)!
       
        ///
        let context = CIContext.init()
        let bitmapImage = context.createCGImage(image, from: extent)
        bitmap.interpolationQuality = .none
        bitmap.scaleBy(x: scale, y: scale)
        bitmap.draw(bitmapImage!, in: extent)
            
        let scaledImage = bitmap.makeImage()
        return UIImage.init(cgImage: scaledImage!)
    }

你可能感兴趣的:(Swift 生成二维码方法 (高清处理生成二维码))