Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
swift UIImageExtension
// MARK: 颜色转UIImage
extension UIImage {
public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1.0, height: 1.0)) {
UIGraphicsBeginImageContextWithOptions(size, true, UIScreen.main.scale)
defer {
UIGraphicsEndImageContext()
}
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(CGRect(origin: CGPoint.zero, size: size))
context?.setShouldAntialias(true)
let image = UIGraphicsGetImageFromCurrentImageContext()
guard let cgImage = image?.cgImage else {
self.init()
return nil
}
self.init(cgImage: cgImage)
}
}
// MARK: 图片设置圆角
extension UIImage {
public func roundImage(byRoundingCorners: UIRectCorner = UIRectCorner.allCorners, cornerRadi: CGFloat) -> UIImage? {
return roundImage(byRoundingCorners: byRoundingCorners, cornerRadii: CGSize(width: cornerRadi, height: cornerRadi))
}
public func roundImage(byRoundingCorners: UIRectCorner = UIRectCorner.allCorners, cornerRadii: CGSize) -> UIImage? {
let imageRect = CGRect(origin: CGPoint.zero, size: size)
UIGraphicsBeginImageContextWithOptions(size, false, scale)
defer {
UIGraphicsEndImageContext()
}
let context = UIGraphicsGetCurrentContext()
guard context != nil else {
return nil
}
context?.setShouldAntialias(true)
let bezierPath = UIBezierPath(roundedRect: imageRect,
byRoundingCorners: byRoundingCorners,
cornerRadii: cornerRadii)
bezierPath.close()
bezierPath.addClip()
self.draw(in: imageRect)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
// MARK: 通过String生成二维码
extension UIImage {
public static func generateQRImage(QRCodeString: String, logo: UIImage?, size: CGSize = CGSize(width: 50, height: 50)) -> UIImage? {
guard let data = QRCodeString.data(using: .utf8, allowLossyConversion: false) else {
return nil
}
let imageFilter = CIFilter(name: "CIQRCodeGenerator")
imageFilter?.setValue(data, forKey: "inputMessage")
imageFilter?.setValue("H", forKey: "inputCorrectionLevel")
let ciImage = imageFilter?.outputImage
// 创建颜色滤镜
let colorFilter = CIFilter(name: "CIFalseColor")
colorFilter?.setDefaults()
colorFilter?.setValue(ciImage, forKey: "inputImage")
colorFilter?.setValue(CIColor(red: 0, green: 0, blue: 0), forKey: "inputColor0")
colorFilter?.setValue(CIColor(red: 1, green: 1, blue: 1), forKey: "inputColor1")
// 返回二维码图片
let qrImage = UIImage(ciImage: (colorFilter?.outputImage)!)
let imageRect = size.width > size.height ?
CGRect(x: (size.width - size.height) / 2, y: 0, width: size.height, height: size.height) :
CGRect(x: 0, y: (size.height - size.width) / 2, width: size.width, height: size.width)
UIGraphicsBeginImageContextWithOptions(imageRect.size, false, UIScreen.main.scale)
defer {
UIGraphicsEndImageContext()
}
qrImage.draw(in: imageRect)
if logo != nil {
let logoSize = size.width > size.height ?
CGSize(width: size.height * 0.25, height: size.height * 0.25) :
CGSize(width: size.width * 0.25, height: size.width * 0.25)
logo?.draw(in: CGRect(x: (imageRect.size.width - logoSize.width) / 2, y: (imageRect.size.height - logoSize.height) / 2, width: logoSize.width, height: logoSize.height))
}
return UIGraphicsGetImageFromCurrentImageContext()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.