swift 编程如何在 UIimage 中设置背景颜色
how to set a background color in UIimage in swift programming
我用drawInRect()
方法画了一张图。
我的矩形是 120*120,我的图片是 100*100。
如何在 swift 中为我的图像设置背景颜色?
最好使用 UIImageView 的 backgroundColor 属性。您可以按如下方式执行此操作 -
self.imageView.backgroundColor = UIColor.redColor()
存在以下预设颜色:
blackColor
darkGrayColor
lightGrayColor
whiteColor
grayColor
redColor
greenColor
blueColor
cyanColor
yellowColor
magentaColor
orangeColor
purpleColor
brownColor
clearColor
如果您想以编程方式创建具有给定颜色的 UIImage,您可以按如下方式执行此操作:
var rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
其中 var
'image' 是您的彩色 UIImage。
您也可以使用此扩展程序:
extension UIImage {
func imageWithColor(tintColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: self.size.height)
context.scaleBy(x: 1.0, y: -1.0);
context.setBlendMode(.normal)
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) as CGRect
context.clip(to: rect, mask: self.cgImage!)
tintColor.setFill()
context.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}
然后
image.imageWithColor("#1A6BAE".UIColor)
编辑:2019-11:由@Yuto
更新为Swift 5
更新了@Egghead 对 Swift 3
的解决方案
extension UIImage {
static func imageWithColor(tintColor: UIColor) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
tintColor.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
用法:
UIImage.imageWithColor(tintColor: <Custom color>)
另一种获取带有扩展名的背景色图像的方法是:(Swift 3)
extension UIImage {
/**
Returns an UIImage with a specified background color.
- parameter color: The color of the background
*/
convenience init(withBackground color: UIColor) {
let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size);
let context:CGContext = UIGraphicsGetCurrentContext()!;
context.setFillColor(color.cgColor);
context.fill(rect)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
self.init(ciImage: CIImage(image: image)!)
}
}
然后:
// change UIColor.white with your color
let image = UIImage(withBackground: UIColor.white)
swift 4 的解决方案基于已接受的答案:
func imageWithColor(tintColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext() as! CGContext
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1, y: -1)
context.setBlendMode(.normal)
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) as CGRect
context.clip(to: rect, mask: self.cgImage!)
tintColor.setFill()
context.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() as! UIImage
UIGraphicsEndImageContext()
return newImage
}
用法:
image.imageWithColor("#1A6BAE".UIColor)
Swift 5, 4
如果需要绘制图片的背景,为了优化和装饰的目的,可以按照特定的方式绘制图片:
UIImage(named: "someImage")?.withBackground(color: .white)
扩展
extension UIImage {
func withBackground(color: UIColor, opaque: Bool = true) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
guard let ctx = UIGraphicsGetCurrentContext(), let image = cgImage else { return self }
defer { UIGraphicsEndImageContext() }
let rect = CGRect(origin: .zero, size: size)
ctx.setFillColor(color.cgColor)
ctx.fill(rect)
ctx.concatenate(CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: size.height))
ctx.draw(image, in: rect)
return UIGraphicsGetImageFromCurrentImageContext() ?? self
}
}
我用drawInRect()
方法画了一张图。
我的矩形是 120*120,我的图片是 100*100。
如何在 swift 中为我的图像设置背景颜色?
最好使用 UIImageView 的 backgroundColor 属性。您可以按如下方式执行此操作 -
self.imageView.backgroundColor = UIColor.redColor()
存在以下预设颜色:
blackColor
darkGrayColor
lightGrayColor
whiteColor
grayColor
redColor
greenColor
blueColor
cyanColor
yellowColor
magentaColor
orangeColor
purpleColor
brownColor
clearColor
如果您想以编程方式创建具有给定颜色的 UIImage,您可以按如下方式执行此操作:
var rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
其中 var
'image' 是您的彩色 UIImage。
您也可以使用此扩展程序:
extension UIImage {
func imageWithColor(tintColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: self.size.height)
context.scaleBy(x: 1.0, y: -1.0);
context.setBlendMode(.normal)
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) as CGRect
context.clip(to: rect, mask: self.cgImage!)
tintColor.setFill()
context.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}
然后
image.imageWithColor("#1A6BAE".UIColor)
编辑:2019-11:由@Yuto
更新为Swift 5更新了@Egghead 对 Swift 3
的解决方案extension UIImage {
static func imageWithColor(tintColor: UIColor) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
tintColor.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
用法:
UIImage.imageWithColor(tintColor: <Custom color>)
另一种获取带有扩展名的背景色图像的方法是:(Swift 3)
extension UIImage {
/**
Returns an UIImage with a specified background color.
- parameter color: The color of the background
*/
convenience init(withBackground color: UIColor) {
let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size);
let context:CGContext = UIGraphicsGetCurrentContext()!;
context.setFillColor(color.cgColor);
context.fill(rect)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
self.init(ciImage: CIImage(image: image)!)
}
}
然后:
// change UIColor.white with your color
let image = UIImage(withBackground: UIColor.white)
swift 4 的解决方案基于已接受的答案:
func imageWithColor(tintColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext() as! CGContext
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1, y: -1)
context.setBlendMode(.normal)
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) as CGRect
context.clip(to: rect, mask: self.cgImage!)
tintColor.setFill()
context.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() as! UIImage
UIGraphicsEndImageContext()
return newImage
}
用法:
image.imageWithColor("#1A6BAE".UIColor)
Swift 5, 4
如果需要绘制图片的背景,为了优化和装饰的目的,可以按照特定的方式绘制图片:
UIImage(named: "someImage")?.withBackground(color: .white)
扩展
extension UIImage {
func withBackground(color: UIColor, opaque: Bool = true) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
guard let ctx = UIGraphicsGetCurrentContext(), let image = cgImage else { return self }
defer { UIGraphicsEndImageContext() }
let rect = CGRect(origin: .zero, size: size)
ctx.setFillColor(color.cgColor)
ctx.fill(rect)
ctx.concatenate(CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: size.height))
ctx.draw(image, in: rect)
return UIGraphicsGetImageFromCurrentImageContext() ?? self
}
}