美文网首页swift
Swift 生成二维码方法 (高清处理生成二维码)

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

作者: AdeSon | 来源:发表于2021-06-09 13:11 被阅读0次
        ///--生成二维码
    
            /// 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 生成二维码方法 (高清处理生成二维码)

        本文链接:https://www.haomeiwen.com/subject/raydeltx.html