如何将颜色乘以 Swift 中的图像?

How to multiply a color to an image in Swift?

我对将我的应用程序从 Android 移动到 iOS 有其他疑问(我认为这更难...)。

好的,所以在这种情况下,我正在尝试为下一张图片着色:

在Android中我使用了下一个代码:

public static Bitmap doNalaFilter (Bitmap src) {
     Bitmap bmBrown0 = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
     Canvas cBrown0 = new Canvas(bmBrown0);
     Paint paintBrown0 = new Paint();
     paintBrown0.setColorFilter(new LightingColorFilter(Color.WHITE, 0x800000));
     cBrown0.drawBitmap(bmTemp, 0, 0, paintBrown0);
     Bitmap bmBrown = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
     Canvas cBrown = new Canvas(bmBrown);
     cBrown.drawBitmap(src, 0, 0, null);
     Paint paintBrown = new Paint();
     paintBrown.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
     cBrown.drawBitmap(bmBrown0, 0, 0, paintBrown);
     return bmBrown;
}

获取下一张图片:

但在 Swift 我正在尝试下一个代码:

func nalaFilter() -> UIImage? {
        let inImage = CIImage (image: self)
        let SRGBImage = inImage?.applyingFilter("CILinearToSRGBToneCurve")

        dynamic let brownMatrix = CIFilter (name: "CIMultiplyBlendMode")

        let brownRect = CGRect (
            x: (SRGBImage?.extent.origin.x)!,
            y: (SRGBImage?.extent.origin.y)!,
            width: (SRGBImage?.extent.size.width)!,
            height: (SRGBImage?.extent.size.height)!)
        let brownColor = CIColor (red: 128.0/255.0, green: 0.0, blue: 0.0)
        let brownOverlay = CIImage (color: brownColor)
        let brownCroppedImage = brownOverlay.cropped(to: brownRect)
        brownMatrix?.setValue(SRGBImage, forKey: kCIInputImageKey)
        brownMatrix?.setValue(brownCroppedImage, forKey: kCIInputBackgroundImageKey)
        let brownOutImage = brownMatrix?.outputImage

        let linearImage = brownOutImage?.applyingFilter("CISRGBToneCurveToLinear")

        let cgImage = CIContext().createCGImage(linearImage!, from: linearImage!.extent)
        return UIImage (cgImage: cgImage!)
}

我明白了!

有没有人知道一个 Swift 代码可以以与 Android 中相同的方式工作?

提前致谢!

最后我得到了解决我问题的下一个代码:

func nalaFilter() -> UIImage? {
        let brownColor = CIColor (red: 128.0/255.0, green: 0.0, blue: 0.0)
        let brownRect = CGRect (origin: .zero, size: self.size)

        //Colored image
        UIGraphicsBeginImageContextWithOptions(brownRect.size, true, 0.0)
        brownColor.setFill()
        UIRectFill(brownRect)
        let brownColoredImage = UIGraphicsGetImageFromCurrentImageContext()
        let brownContext = UIGraphicsGetCurrentContext()
        brownContext!.setFillColor(UIColor.white.cgColor)
        brownContext!.fill(brownRect)
        self.draw(in: brownRect, blendMode: .normal, alpha: 1)
        brownColoredImage?.draw(in: brownRect, blendMode: .colorDodge, alpha: 1)
        let outBrown0Image = UIGraphicsGetImageFromCurrentImageContext()

        //Multiplied image
        self.draw(in: brownRect, blendMode: .normal, alpha: 1)
        outBrown0Image?.draw(in: brownRect, blendMode: .multiply, alpha: 1)
        let outBrownImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return outBrownImage
}

这得到的结果与我的 Android 完全相同。