我使用drawInRect()
方法绘制图像。
我的矩形尺寸为120 * 120,我的图像为100 * 100。
如何在swift中为我的图像设置背景颜色?
答案 0 :(得分:7)
您也可以使用此扩展程序:
extension UIImage {
func imageWithColor(tintColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext() as CGContextRef
CGContextTranslateCTM(context, 0, self.size.height)
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal)
let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
CGContextClipToMask(context, rect, self.CGImage)
tintColor.setFill()
CGContextFillRect(context, rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
return newImage
}
}
然后
image.imageWithColor("#1A6BAE".UIColor)
答案 1 :(得分:6)
更新了@ 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>)
答案 2 :(得分:4)
最好使用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
'图片'是你的彩色UIImage。
答案 3 :(得分:3)
获取具有扩展名的背景颜色的图像的另一种方法是:(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)
答案 4 :(得分:2)
如果需要绘制图像的背景(出于优化和装饰目的),可以以特定方式绘制图像:
UIImage(named: "someImage")?.withBackground(color: .white)
extension UIImage {
func withBackground(color: UIColor, opaque: Bool = true) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
guard let ctx = UIGraphicsGetCurrentContext() 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(cgImage!, in: rect)
return UIGraphicsGetImageFromCurrentImageContext() ?? self
}
}
答案 5 :(得分:0)
基于已经接受的答案的“快速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)