Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MonoTouch创建自定义图像过滤器_C#_Ios_Image Processing_Xamarin.ios_Xamarin - Fatal编程技术网

C# 使用MonoTouch创建自定义图像过滤器

C# 使用MonoTouch创建自定义图像过滤器,c#,ios,image-processing,xamarin.ios,xamarin,C#,Ios,Image Processing,Xamarin.ios,Xamarin,我正在使用Xamarin for iOS动态创建合成图像。我有两个图像,一个是基础图像(黑白),另一个是用于滤色器的alpha遮罩“贴图”。然后,在运行时,提供一个RGB值,这样我就可以使合成图像成为任何颜色 我已经找到了很多更改整个图像的解决方案,但是我很难找到一个好的解决方案来创建只对遮罩贴图中包含的区域(使用alpha值)着色的颜色过滤器: 基本图像: 结果图像(如果我告诉它我想要红色): 在MonoDroid中,我能够创建一个自定义的ImageView并覆盖OnDraw方法,然后使用

我正在使用Xamarin for iOS动态创建合成图像。我有两个图像,一个是基础图像(黑白),另一个是用于滤色器的alpha遮罩“贴图”。然后,在运行时,提供一个RGB值,这样我就可以使合成图像成为任何颜色

我已经找到了很多更改整个图像的解决方案,但是我很难找到一个好的解决方案来创建只对遮罩贴图中包含的区域(使用alpha值)着色的颜色过滤器:

基本图像:

结果图像(如果我告诉它我想要红色):

在MonoDroid中,我能够创建一个自定义的ImageView并覆盖OnDraw方法,然后使用ColorMatrixColorFilter,它工作起来很有魅力,但不知道如何在MonoTouch中完成同样的事情

这就是我为MonoDroid所做的:

    protected override void OnDraw(Canvas canvas)
    {
        if (_alphaMask == null)
            _alphaMask = BitmapFactory.DecodeResource(Resources, GetDrawable("star_mask"));

        if(_image == null)
            _image = BitmapFactory.DecodeResource(Resources, GetDrawable("star"));

        AdjustColor(_color, canvas, _alphaMask, _image);
    }

    private static void AdjustColor(Color color, Canvas c, Bitmap alphaMask, Bitmap image)
    {
        float R = color.R;
        float G = color.G;
        float B = color.B;
        var maskPaint = new Paint();
        var mat = new[]
        {
            0, 0, 0, 0, R, //red
            0, 0, 0, 0, G, //green
            0, 0, 0, 0, B, //blue
            0, 0, 0, 1, 0  //alpha
        };
        ColorMatrix cm = new ColorMatrix(mat);
        maskPaint.SetColorFilter(new ColorMatrixColorFilter(cm));
        c.DrawBitmap(image, 0, 0, null);
        c.DrawBitmap(alphaMask, 0, 0, maskPaint);
    }

苹果开发者网站:


似乎是您正在寻找的(CIFilter)。

好的。。。不是最优雅的解决方案,但我创建了两个
ImageView

在基础图像的顶部是
MaskView
,然后使用以下方法创建彩色
UIImage

private UIImage GetColoredImage(string imageName, Color color)
    {
        UIImage image = UIImage.FromBundle(imageName);

        UIGraphics.BeginImageContext(image.Size);
        CGContext context = UIGraphics.GetCurrentContext();

        context.TranslateCTM(0, image.Size.Height);
        context.ScaleCTM(1.0f, -1.0f);

        var rect = new RectangleF(0, 0, image.Size.Width, image.Size.Height);

        // draw image, (to get transparancy mask)
        context.SetBlendMode(CGBlendMode.Normal);
        context.DrawImage(rect, image.CGImage);

        // draw the color using the sourcein blend mode so its only draw on the non-transparent pixels
        context.SetBlendMode(CGBlendMode.SourceIn);
        context.SetFillColor(color.R / 255f, color.G / 255f, color.B / 255f, 1);
        context.FillRect(rect);

        UIImage coloredImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
        return coloredImage;
    }

如果有人有更好的解决方案(可能使用
CIFilter
),我很乐意看到它。

这是一个正确的方向,但我从Objective-C到C的转换能力不是很好。下面是一个用C创建CIFilter的快速示例: