gpt4 book ai didi

ios - swift 改变图像的色调

转载 作者:搜寻专家 更新时间:2023-10-31 22:26:52 25 4
gpt4 key购买 nike

我是 swift 的新手,并试图从根本上实现这一点,这张图片给 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)!
}

如果这是一个菜鸟问题,抱歉。我能够在 javascript 中轻松地做到这一点,但不是很快。

最佳答案

您好,您可以通过以下方式使用它。首先,您使用的 UIImage 扩展需要一些更新。你可以复制下面的 Swift 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))

关于ios - swift 改变图像的色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com