import UIKit
extension UIImage {
class func createQRCodeImage(content: String, size: CGSize) -> UIImage {
let stringData = content.data(using: String.Encoding.utf8)
let qrFilter = CIFilter(name: \"CIQRCodeGenerator\")
qrFilter?.setValue(stringData, forKey: \"inputMessage\")
qrFilter?.setValue(\"H\", forKey: \"inputCorrectionLevel\")
let colorFilter = CIFilter(name: \"CIFalseColor\")
colorFilter?.setDefaults()
colorFilter?.setValuesForKeys([\"inputImage\" : (qrFilter?.outputImage)!,\"inputColor0\":CIColor.init(cgColor: UIColor.black.cgColor),\"inputColor1\":CIColor.init(cgColor: UIColor.white.cgColor)])
let qrImage = colorFilter?.outputImage
let cgImage = CIContext(options: nil).createCGImage(qrImage!, from: (qrImage?.extent)!)
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
context!.interpolationQuality = .none
context!.scaleBy(x: 1.0, y: -1.0)
context?.draw(cgImage!, in: (context?.boundingBoxOfClipPath)!)
let codeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return codeImage!
}
}
extension UIImage {
class func createImageFrom(color: UIColor, width: CGFloat = 1, height: CGFloat = 1) -> UIImage? {
let rect = CGRect(x: 0.0, y: 0.0, width: width, height: height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image ?? nil
}
class func createImageFrom(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage? {
let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image ?? nil
}
}
继续阅读与本文标签相同的文章
上一篇 :
邮箱自动补充
下一篇 :
TPP多租户隔离之资源清理
-
每分钟进出车辆2.5台 智能立体车库解锁停车难
2026-05-19栏目: 教程
-
一文了解机器学习必学10大算法
2026-05-19栏目: 教程
-
开一家线上外卖门店选址要注意哪些因素?
2026-05-19栏目: 教程
-
信院人的APP,你get到了吗?
2026-05-19栏目: 教程
-
对话FILA姚伟雄:安踏赋予独立性,未来坚持做直营
2026-05-19栏目: 教程
