19
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Swift】UIImageの色を変える(塗りつぶす)

Last updated at Posted at 2015-04-24

こんなシンプルな内容

2018/01/18 更新

Swift4

UIImageExtension.swift
extension UIImage {
    func tint(color: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        let drawRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIRectFill(drawRect)
        draw(in: drawRect, blendMode: .destinationIn, alpha: 1)
        let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return tintedImage
    }
}

Swift2

UIImageExtension.swift
extension UIImage {
	func tint(color: UIColor) -> UIImage {
		UIGraphicsBeginImageContextWithOptions(size, false, 0)
		color.setFill()
		let drawRect = CGRectMake(0, 0, size.width, size.height)
		UIRectFill(drawRect)
		drawInRect(drawRect, blendMode: kCGBlendModeDestinationIn, alpha: 1)
		let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
		UIGraphicsEndImageContext()
		return tintedImage
	}
}

同じ画像の色違いを表示したい時には使える関数だと思います。

19
16
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?