如何使用 Swift 在现有图像中添加背景颜色?
How to add background colour in existing image using Swift?
我正在尝试为现有图像添加背景颜色,但它不起作用。
下面是我的代码:-
extension UIImage {
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()
//self.type(of: init)(ciImage: CIImage(image: image)!)
return image
}
}
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)!)
}
}.
我是图形方面的新手,不知道如何为图像添加背景颜色。
在这个白色图像中,橙色是背景色。
您应该使用背景颜色和现有图像创建一个新图像。
试试这个。
func getImage(image: UIImage, backgroundColor: UIColor)->UIImage?{
UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
backgroundColor.setFill()
//UIRectFill(CGRect(origin: .zero, size: image.size))
let rect = CGRect(origin: .zero, size: image.size)
let path = UIBezierPath(arcCenter: CGPoint(x:rect.midX, y:rect.midY), radius: rect.midX, startAngle: 0, endAngle: 6.28319, clockwise: true)
path.fill()
image.draw(at: .zero)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
我正在尝试为现有图像添加背景颜色,但它不起作用。
下面是我的代码:-
extension UIImage {
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()
//self.type(of: init)(ciImage: CIImage(image: image)!)
return image
}
}
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)!)
}
}.
我是图形方面的新手,不知道如何为图像添加背景颜色。
在这个白色图像中,橙色是背景色。
您应该使用背景颜色和现有图像创建一个新图像。 试试这个。
func getImage(image: UIImage, backgroundColor: UIColor)->UIImage?{
UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
backgroundColor.setFill()
//UIRectFill(CGRect(origin: .zero, size: image.size))
let rect = CGRect(origin: .zero, size: image.size)
let path = UIBezierPath(arcCenter: CGPoint(x:rect.midX, y:rect.midY), radius: rect.midX, startAngle: 0, endAngle: 6.28319, clockwise: true)
path.fill()
image.draw(at: .zero)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}