2017-07-26 125 views
2

色調我是新來的迅速和努力基本上實現這一目標, 此圖片 enter image description here改變圖像的迅速

此圖片 - >

enter image description here

我用這代碼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中輕鬆完成此操作,但不能快速完成。

+1

其imageview或多個圖像 –

+0

此圖像正在作爲素材應用 –

回答

2

您好,您可以通過以下方式使用它。首先你使用的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)) 
+0

我使用了這種方法。我用綠色填充整個圖像。我嘗試了multiply,sourceIn,sourceOut作爲混合模式,但都具有相同的效果......我如何才能獲得僅用於更改顏色的線條?原始圖像alpha必須相乘? –

+0

您使用的是.png或.jpg嗎? 您還必須確保圖像在框中是透明的,只有線條可見,因此該效果僅適用於線條 –

+0

這是一個png ..線條之間的空間是透明的(alpha 0) –

0

嘗試下面的代碼,應該爲你工作。

if let myImage = UIImage(named: "imageName")?.withRenderingMode(.alwaysTemplate) { 
     myImageView.image = myImage 
     myImageView.tintColor = UIColor.white 
    } 
0

您的圖像很簡單,只有一種顏色。我會建議讓圖像透明,除了行的位置,然後將其分層在白色背景之上以獲得結果。

它看起來像你在使用繪圖模式後得到的結果。還有一些核心圖像濾鏡,可以將圖像着色效果應用於圖像以及替換特定顏色。對於更復雜的圖像來說,這將是一個不錯的選擇。

+0

圖像除線條外透明。 SO在這裏顯示它,填充白色。 –