2011-12-20 135 views
10

我正在嘗試從彩色PNG和alpha製作模糊灰度UIImage的配方。在ObjC中有食譜,但是MonoTouch不綁定CGRect函數,所以不知道如何做到這一點。有任何想法嗎?MonoTouch將alpha彩色UIImage轉換爲灰度和模糊?

這裏是灰度的一個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; 
} 
+0

我曾嘗試使用此功能,但它不會比1.0規模...(符其他圖像工作在視網膜上) – Jonny 2013-06-05 03:14:58

回答

13

MonoTouch的不綁定CGRect功能,所以不知道如何做到這一點。

當使用MonoTouch CGRect映射到RectangleF。存在很多擴展方法,應該映射到由GCRect提供的每個功能。使用它們移植ObjectiveC代碼時,您應該沒有任何問題。

如果缺少某些內容,請填寫錯誤報告@http://bugzilla.xamarin.com,我們將盡快解決(並儘可能提供解決方法)。

有食譜赫然出現在ObjC但MonoTouch的

如果您有聯繫,那麼請編輯您的問題,並添加它們。這將使它更容易幫助你:)

UPDATE

這裏有一個,行由行,你的榜樣的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); 
     } 
    } 
+0

工作偉大!我想我可能需要花更多的時間來學習翻譯ObjC的例子,因爲顯然它可以變得很容易(就像你剛纔那樣)。謝謝 – 2011-12-20 19:23:46

+0

很高興爲您服務:-)請花時間接受答案(綠色標記下方的選票號碼),以允許其他人,尋找類似的問題,看到答案是可用的。 – poupou 2011-12-20 19:27:07

0

我寫的模糊和色調的UIImage類的本地端口從WWDC對MonoTouch的更容易我的眼睛。對於色彩

示例代碼和模糊:

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

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

您應該只將deltaSaturationFactor設置爲0 – evya 2017-07-03 10:12:04