Created
October 21, 2017 06:18
-
-
Save 5oya/9b4fe119224dd1ba01f19514c3dbb01d to your computer and use it in GitHub Desktop.
【iOS】指定画像の色を変更した新しい画像を生成する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIImage { | |
static func differentColor(originalImage: UIImage, color: UIColor) -> UIImage { | |
let imageSize = originalImage.size | |
let rect = CGRect(origin: .zero, size: imageSize) | |
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0) | |
color.setFill() | |
UIRectFill(rect); | |
originalImage.draw(in: rect, blendMode: .destinationIn, alpha: 1) | |
guard let tmpImage = UIGraphicsGetImageFromCurrentImageContext(), let cgImage = tmpImage.cgImage else { | |
return UIImage() | |
} | |
let image = UIImage(cgImage: cgImage, scale: originalImage.scale, orientation: originalImage.imageOrientation) | |
let resultImage = image.resizableImage(withCapInsets: originalImage.capInsets, resizingMode: originalImage.resizingMode) | |
UIGraphicsEndImageContext() | |
return resultImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment