onoTouch将彩色UIImage转换为alpha到灰度和模糊

onoTouch将彩色UIImage转换为alpha到灰度和模糊

本文介绍了MonoTouch将彩色UIImage转换为alpha到灰度和模糊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从颜色PNG和alpha中找到用于生成模糊灰度 UIImage 的配方。 ObjC中有配方,但MonoTouch不绑定 CGRect 函数,所以不知道如何做到这一点。有什么想法?

I am trying to find a recipe for producing a a blurred grayscale UIImage from a color PNG and alpha. There are recipes out there in ObjC but MonoTouch does not bind the CGRect functions so not sure how to do this. Any ideas?

以下是灰度的一个ObjC示例:

Here is one ObjC example of grayscale:

(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;
}


推荐答案

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

When using MonoTouch CGRect is mapped to RectangleF. A lot of extension methods exists that should map to every function provided by GCRect. You should not have any problem in porting ObjectiveC code using them.

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

If something is missing please fill a bug report @ http://bugzilla.xamarin.com and we'll fix it asap (and provide a workaround when possible).

如果您有链接,请编辑您的问题并添加。这样可以更方便地帮助您:)

If you have links then please edit your question and add them. That will make it easier to help you :)

更新

这是一个,逐行,C#转换你的例子。它似乎对我有用(而且它比Objective-C更容易;)

Here's a, line-by-line, C# translation of your example. It seems to works for me (and it's much easier to my eyes than 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);
        }
    }

这篇关于MonoTouch将彩色UIImage转换为alpha到灰度和模糊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 11:14