9

我正在尝试找到一种UIImage从颜色 PNG 和 alpha 生成模糊灰度的方法。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;
}
4

2 回答 2

13

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);
        }
    }
于 2011-12-20T16:02:43.790 回答
0

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

色调和模糊的示例代码:

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

UIImage yourImage;
yourImage.ApplyBlur (20f /*blurRadius*/, tintColor, 1.8f /*deltaSaturationFactor*/, null);
于 2014-02-17T20:44:37.113 回答