[从iOS 13.0+开始,UIImage class has method withTintColor。
iOS 12.4上的等效功能如何,以支持不能运行iOS 13的旧版iPhone?
[总体目标
是绘制带有黑色或白色发光效果(深色或浅色主题)的着色图像。这是Xamarin.Forms.Platform.iOS.PlatformEffect的一部分。
Xamarin C#iOS
代码将着色图像覆盖的光晕绘制为位图-在iOS 13上有效:private static UIImage ApplyGlow2( UIImage src, double radius, Xamarin.Forms.Color color, Xamarin.Forms.Color? tintColor, int outW, int outH) { int glowRadius = (int)radius; // Increase size of the initial image. int marginX2 = glowRadius * 2; int margin = glowRadius; // Resize original image to display size. UIGraphics.BeginImageContext(new CGSize(outW, outH)); src.Draw(new CGRect(0, 0, outW, outH)); src = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); // Add margins. UIGraphics.BeginImageContext(new CGSize(outW + marginX2, outH + marginX2)); CGContext context = UIGraphics.GetCurrentContext(); CGRect drawRect = new CGRect(margin, margin, outW, outH); src.Draw(drawRect); src = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); // Create color overlay. UIGraphics.BeginImageContext(new CGSize(outW + marginX2, outH + marginX2)); context = UIGraphics.GetCurrentContext(); drawRect = new CGRect(0, 0, outW + marginX2, outH + marginX2); context.TranslateCTM(0, outH + marginX2); context.ScaleCTM(1.0f, -1.0f); context.SetBlendMode(CGBlendMode.Normal); context.SetFillColor(color.ToCGColor()); context.FillRect(drawRect); context.SetBlendMode(CGBlendMode.DestinationIn); context.DrawImage(drawRect, src.CGImage); UIImage ret = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); // Create blurred image; // create a CIGaussianBlur filter with the input image. CIGaussianBlur gaussian_blur = new CIGaussianBlur() { InputImage = ret.CGImage, Radius = (float)radius * 0.5f, }; CIImage output = gaussian_blur.OutputImage; CIContext context2 = CIContext.FromOptions(null); CGImage cgimage = context2.CreateCGImage(output, new CGRect(new CGPoint(0,0), ret.Size)); UIImage blurredImage = UIImage.FromImage(cgimage); // -- Merge blur with original. -- UIGraphics.BeginImageContext(new CGSize(outW + marginX2, outH + marginX2)); //CGContext context3 = UIGraphics.GetCurrentContext(); // Blur. CGRect drawRect2 = new CGRect(0, 0, outW + marginX2, outH + marginX2); CGRect drawRect3 = new CGRect(1, 1, outW + marginX2, outH + marginX2); CGRect drawRect4 = new CGRect(2, 2, outW + marginX2, outH + marginX2); blurredImage.Draw(drawRect2); blurredImage.Draw(drawRect3); blurredImage.Draw(drawRect4); if (tintColor != null) { // Draw tinted original (over blur). var tintedImage = src.ApplyTintColor( tintColor.Value.ToUIColor() ); tintedImage.Draw( drawRect2 ); } else { // Draw original (over blur). src.Draw( drawRect2 ); } ret = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return ret; }
上方与我的问题最相关的行:
// Draw tinted original (over blur). var tintedImage = src.ApplyTintColor( tintColor.Value.ToUIColor() ); tintedImage.Draw( drawRect2 );
我尝试过的事情:
iOS 12确实具有可以在渲染到UIImageView时应用的TintColor,但是当以编程方式绘制到UIImage中时,我没有看到使用该颜色的方法:
// Apply tint color. UIImageView Control. Control.Image = Control.Image.ImageWithRenderingMode(UIKit.UIImageRenderingMode.AlwaysTemplate); Control.TintColor = ((TintedImage)Element).TintColor.ToUIColor();
上面的代码类似于Using Tint color on UIImageView,这是我所看到的最接近的SO帖子。
[还有其他几篇类似的文章,但是AFAIK都使用uiimageview.tintColor
,并且不添加与绘制位图有关的任何其他信息(不是视图)。How to apply a tintColor to a UIImage?
[如果有某种方法可以创建不属于视图层次结构的UIView
,请设置其TintColor,然后将其渲染为位图,这可能是解决此问题的一种方法。但是我不知道该怎么做。这种方法的一个步骤类似于这些帖子:
我正在通过Xamarin.iOS进行编程,但是可以接受任何iOS语言的解决方案。
[从iOS 13.0+开始,UIImage类具有withTintColor的方法。如何在iOS 12.4上实现等效功能,以支持无法运行iOS 13的旧版iPhone?总体目标是绘制有色图像,并使用...
((Xamarin C#iOS)