改变图像的迅速
色调我是新来的迅速和努力基本上实现这一目标, 此图片 改变图像的迅速
此图片 - >
我用这代码from here改变图像的色调,但没有得到所需的输出
func tint(image: UIImage, color: UIColor) -> UIImage
{
let ciImage = CIImage(image: image)
let filter = CIFilter(name: "CIMultiplyCompositing")
let colorFilter = CIFilter(name: "CIConstantColorGenerator")
let ciColor = CIColor(color: color)
colorFilter.setValue(ciColor, forKey: kCIInputColorKey)
let colorImage = colorFilter.outputImage
filter.setValue(colorImage, forKey: kCIInputImageKey)
filter.setValue(ciImage, forKey: kCIInputBackgroundImageKey)
return UIImage(CIImage: filter.outputImage)!
}
如果这是一个noob问题,道歉。我能够在JavaScript中轻松完成此操作,但不能快速完成。
您好,您可以通过以下方式使用它。首先你使用的UIImage扩展需要一些更新。 您可以斯威夫特3
extension UIImage{
func tint(color: UIColor, blendMode: CGBlendMode) -> UIImage
{
let drawRect = CGRect(x: 0,y: 0,width: size.width,height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, scale)
color.setFill()
UIRectFill(drawRect)
draw(in: drawRect, blendMode: blendMode, alpha: 1.0)
let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return tintedImage!
}
}
然后在您的viewDidLoad中,你所使用的图像 例如下面的代码复制我使用IBOutlet中imageWood
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.imageWood.image = self.imageWood.image?.tint(color: UIColor.green, blendMode: .saturation)
}
像你将不得不使用合适的颜色和图像
另一Extension我发现
extension UIImage {
// colorize image with given tint color
// this is similar to Photoshop's "Color" layer blend mode
// this is perfect for non-greyscale source images, and images that have both highlights and shadows that should be preserved
// white will stay white and black will stay black as the lightness of the image is preserved
func tint(_ tintColor: UIColor) -> UIImage {
return modifiedImage { context, rect in
// draw black background - workaround to preserve color of partially transparent pixels
context.setBlendMode(.normal)
UIColor.black.setFill()
context.fill(rect)
// draw original image
context.setBlendMode(.normal)
context.draw(self.cgImage!, in: rect)
// tint image (loosing alpha) - the luminosity of the original image is preserved
context.setBlendMode(.color)
tintColor.setFill()
context.fill(rect)
// mask by alpha values of original image
context.setBlendMode(.destinationIn)
context.draw(self.cgImage!, in: rect)
}
}
// fills the alpha channel of the source image with the given color
// any color information except to the alpha channel will be ignored
func fillAlpha(_ fillColor: UIColor) -> UIImage {
return modifiedImage { context, rect in
// draw tint color
context.setBlendMode(.normal)
fillColor.setFill()
context.fill(rect)
// mask by alpha values of original image
context.setBlendMode(.destinationIn)
context.draw(self.cgImage!, in: rect)
}
}
fileprivate func modifiedImage(_ draw: (CGContext, CGRect) ->()) -> UIImage {
// using scale correctly preserves retina images
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let context: CGContext! = UIGraphicsGetCurrentContext()
assert(context != nil)
// correctly rotate image
context.translateBy(x: 0, y: size.height);
context.scaleBy(x: 1.0, y: -1.0);
let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
draw(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
使用它像这样
self.imageWood.image = self.imageWood.image?.tint(UIColor.purple.withAlphaComponent(1))
我使用了这种方法。我用绿色填充整个图像。我尝试了multiply,sourceIn,sourceOut作为混合模式,但都具有相同的效果......我如何才能获得仅用于更改颜色的线条?原始图像alpha必须相乘? –
您使用的是.png或.jpg吗? 您还必须确保图像在框中是透明的,只有线条可见,因此该效果仅适用于线条 –
这是一个png ..线条之间的空间是透明的(alpha 0) –
尝试下面的代码,应该为你工作。
if let myImage = UIImage(named: "imageName")?.withRenderingMode(.alwaysTemplate) {
myImageView.image = myImage
myImageView.tintColor = UIColor.white
}
您的图像很简单,只有一种颜色。我会建议让图像透明,除了行的位置,然后将其分层在白色背景之上以获得结果。
它看起来像你在使用绘图模式后得到的结果。还有一些核心图像滤镜,可以将图像着色效果应用于图像以及替换特定颜色。对于更复杂的图像来说,这将是一个不错的选择。
图像除线条外透明。 SO在这里显示它,填充白色。 –
其imageview或多个图像 –
此图像正在作为素材应用 –