Skip to content

Instantly share code, notes, and snippets.

@5oya
Created October 21, 2017 06:18
Show Gist options
  • Save 5oya/9b4fe119224dd1ba01f19514c3dbb01d to your computer and use it in GitHub Desktop.
Save 5oya/9b4fe119224dd1ba01f19514c3dbb01d to your computer and use it in GitHub Desktop.
【iOS】指定画像の色を変更した新しい画像を生成する
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