【问题标题】:MonoTouch convert color UIImage with alpha to grayscale and blur?MonoTouch 将带有 alpha 的颜色 UIImage 转换为灰度和模糊?
【发布时间】:2011-12-20 15:13:56
【问题描述】:

我正在尝试从颜色 PNG 和 alpha 中找到生成模糊灰度 UIImage 的配方。 ObjC 中有一些配方,但 MonoTouch 不绑定 CGRect 函数,因此不知道如何执行此操作。有什么想法吗?

这是一个 ObjC 灰度示例:

(UIImage *)convertImageToGrayScale:(UIImage *)image
{
  // Create image rectangle with current image width/height
  CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);

  // Grayscale color space
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

  // Create bitmap content with current image size and grayscale colorspace
  CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0,   colorSpace, kCGImageAlphaNone);

  // Draw image into current context, with specified rectangle
  // using previously defined context (with grayscale colorspace)
  CGContextDrawImage(context, imageRect, [image CGImage]);

  // Create bitmap image info from pixel data in current context
  CGImageRef imageRef = CGBitmapContextCreateImage(context);

  // Create a new UIImage object  
  UIImage *newImage = [UIImage imageWithCGImage:imageRef];

  // Release colorspace, context and bitmap information
  CGColorSpaceRelease(colorSpace);
  CGContextRelease(context);
  CFRelease(imageRef);

  // Return the new grayscale image
  return newImage;
}

【问题讨论】:

  • 我尝试使用此功能,但它不适用于 1.0 比例以外的图像...(视网膜断裂)

标签: ios xamarin.ios uiimage grayscale cgrect


【解决方案1】:

Monotouch 不绑定CGRect 函数,所以不知道怎么做。

使用 MonoTouch 时,CGRect 映射到 RectangleF。存在许多扩展方法,它们应该映射到GCRect 提供的每个函数。使用它们移植 ObjectiveC 代码应该没有任何问题。

如果缺少某些内容,请填写错误报告@http://bugzilla.xamarin.com,我们会尽快修复它(并在可能的情况下提供解决方法)。

在 ObjC 中有一些食谱,但 MonoTouch

如果您有链接,请编辑您的问题并添加它们。这将使您更容易为您提供帮助:)

更新

这是您示例的逐行 C# 翻译。它似乎对我有用(而且在我看来它比 Objective-C 容易得多;-)

    UIImage ConvertToGrayScale (UIImage image)
    {
        RectangleF imageRect = new RectangleF (PointF.Empty, image.Size);
        using (var colorSpace = CGColorSpace.CreateDeviceGray ())
        using (var context = new CGBitmapContext (IntPtr.Zero, (int) imageRect.Width, (int) imageRect.Height, 8, 0, colorSpace, CGImageAlphaInfo.None)) {
            context.DrawImage (imageRect, image.CGImage);
            using (var imageRef = context.ToImage ())
                return new UIImage (imageRef);
        }
    }

【讨论】:

  • 效果很好!我想我可能需要花更多的时间来学习翻译 ObjC 示例,因为显然它可以看起来很简单(就像你刚才所做的那样)。谢谢
  • 很高兴它对您有用 :-) 请花时间接受答案(选票下方的绿色标记),以便其他人在搜索类似问题时看到可用的答案。
【解决方案2】:

我为 Monotouch 编写了来自 WWDC 的模糊和色调 UIImage 类别的本地端口。

色调和模糊的示例代码:

UIColor tintColor = UIColor.FromWhiteAlpha (0.11f, 0.73f);

UIImage yourImage;
yourImage.ApplyBlur (20f /*blurRadius*/, tintColor, 1.8f /*deltaSaturationFactor*/, null);

【讨论】:

  • 您应该只将 deltaSaturationFactor 设置为 0
猜你喜欢
  • 2017-08-19
  • 1970-01-01
  • 2011-09-08
  • 2014-01-11
  • 2014-01-08
  • 2015-07-21
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多