2017-07-26 5 views
2

における画像の色合いを変える - >この画像は <a href="https://i.stack.imgur.com/QjBT8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QjBT8.png" alt="enter image description here"></a></p> <p>に 、この画像を私が迅速かつ基本的にこれを達成しようとしているに新しいです迅速

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)! 
} 

これはノブの質問であれば、謝罪します。私はこれをjavascriptで簡単に行うことができましたが、迅速にはできませんでした。

+1

あなたの画像のビューまたは複数の画像 –

+0

この画像は素材として適用されています –

答えて

2

こんにちは、次のように使用できます。最初に使用したUIImage Extensionにはいくつかの更新が必要です。 あなたはたとえば画像 を使用している場所をスウィフト3あなたのviewDidLoadで次に

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! 
} 
} 

については、以下のコードをコピーすることができ、私は

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) 
} 

IBOutlet imageWoodからの画像を使用あなたは使用する必要があります適切な色と画像

Extension iが

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

このメソッドを使用しました。私は画像全体を緑色で塗りつぶしています。私は、混合モードとしてsourceIn、sourceInを乗算しようとしましたが、すべて同じ効果を持っています...どのように色を変更するだけの行を取得できますか?元の画像のアルファは右に乗算する必要がありますか? –

+0

.pngまたは.jpgを使用していますか? また、画像がボックス内で透明であることを確認してください。その結果、線だけが表示され、効果は線の上にのみ適用されます。 –

+0

pngです。線の間隔は透明です(アルファ0) –

0

のようにそれを使用します。

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

あなたの画像はシンプルで、1色しかありません。私は、あなたのイメージを透明にすることを示唆しています。ただし、行がどこにあるのかを除いて、白い背景の上に重ねて結果を得てください。

描画モードを使用した結果が得られたようです。特定の色を置き換えるだけでなく、画像に色合い効果を適用するためのコアイメージフィルタもいくつかあります。より複雑な画像に適しています。

+0

画像は、線を除いて透明です。それで、ここに白い色で塗りつぶしています。 –

関連する問題

 関連する問題