MonoTouch 将带有 alpha 的彩色 UIImage 转换为灰度并模糊?
我正在尝试找到从彩色 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;
}
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?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 MonoTouch 时,
CGRect
映射到RectangleF
。存在许多扩展方法,它们应该映射到 GCRect 提供的每个函数。使用它们移植 ObjectiveC 代码应该不会有任何问题。如果缺少某些内容,请填写错误报告@http://bugzilla.xamarin.com,我们将尽快修复它(并提供如果可能的话解决办法)。
如果您有链接,请编辑您的问题并添加它们。这将使您更容易获得帮助:)
更新
这是您的示例的逐行 C# 翻译。它似乎对我有用(而且在我看来它比 Objective-C 容易得多;-)
When using MonoTouch
CGRect
is mapped toRectangleF
. A lot of extension methods exists that should map to every function provided byGCRect
. 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 :)
UPDATE
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 ;-)
我从 WWDC 为 Monotouch 编写了模糊和色调 UIImage 类别的本机端口。
色调和模糊的示例代码:
I wrote a native port of the blur and tint UIImage categories from WWDC for Monotouch.
Sample code for tint and blur: