Skip to content

Instantly share code, notes, and snippets.

@conceptdev
Created September 20, 2012 01:58
Show Gist options
  • Save conceptdev/3753541 to your computer and use it in GitHub Desktop.
Save conceptdev/3753541 to your computer and use it in GitHub Desktop.
CoreImage new filters in iOS6 (for MonoTouch)
using System;
using MonoTouch.CoreImage;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
// CoreImage Filter Reference
//https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CoreImageFilterReference/Reference/reference.html
namespace CoreImage
{
/// <summary>
/// This class contains the code shown in the article
/// </summary>
public class SampleCode_iOS6
{
/// <summary>
/// Example using a filter to add bloom to an image
/// </summary>
public static void Bloom(UITextView display, UIImageView imgview)
{
var uiimage = UIImage.FromFile ("photo.JPG");
var ciimage = new CIImage (uiimage);
var bloom = CIFilter.FromName ("CIBloom");
bloom [CIFilterInputKey.Image] = ciimage;
bloom [new NSString ("inputRadius")] = new NSNumber(10);
bloom [new NSString ("inputIntensity")] = new NSNumber(3);
var output = bloom.OutputImage;
var context = CIContext.FromOptions(null);
var cgimage = context.CreateCGImage (output, output.Extent);
var ui = UIImage.FromImage (cgimage);
imgview.Image = ui;
}
/// <summary>
/// Example using a filter to invert the color of an image
/// </summary>
public static void ColorInvert(UITextView display, UIImageView imgview)
{
var uiimage = UIImage.FromFile ("photo.JPG");
var ciimage = new CIImage (uiimage);
var colorInvert = CIFilter.FromName ("CIColorInvert");
colorInvert [CIFilterInputKey.Image] = ciimage;
var output = colorInvert.OutputImage;
var context = CIContext.FromOptions(null);
var cgimage = context.CreateCGImage (output, output.Extent);
var ui = UIImage.FromImage (cgimage);
imgview.Image = ui;
}
/// <summary>
/// Example using a filter
/// </summary>
public static void ColorPosterize(UITextView display, UIImageView imgview)
{
var uiimage = UIImage.FromFile ("photo.JPG");
var ciimage = new CIImage (uiimage);
var colorMonochrome = CIFilter.FromName ("CIColorPosterize");
colorMonochrome [CIFilterInputKey.Image] = ciimage;
colorMonochrome [new NSString ("inputLevels")] = new NSNumber(6);
var output = colorMonochrome.OutputImage;
var context = CIContext.FromOptions(null);
var cgimage = context.CreateCGImage (output, output.Extent);
var ui = UIImage.FromImage (cgimage);
imgview.Image = ui;
}
/// <summary>
/// Example using a filter
/// </summary>
public static void PerspectiveTransform(UITextView display, UIImageView imgview)
{
var uiimage = UIImage.FromFile ("photo.JPG");
var ciimage = new CIImage (uiimage);
var prespectiveTransform = CIFilter.FromName ("CIPerspectiveTransform");
prespectiveTransform [CIFilterInputKey.Image] = ciimage;
prespectiveTransform [new NSString ("inputBottomLeft")] = new CIVector(20, 20);
prespectiveTransform [new NSString ("inputBottomRight")] = new CIVector(480, 33);
prespectiveTransform [new NSString ("inputTopRight")] = new CIVector(490, 490);
prespectiveTransform [new NSString ("inputTopLeft")] = new CIVector(30, 400);
var output = prespectiveTransform.OutputImage;
var context = CIContext.FromOptions(null);
var cgimage = context.CreateCGImage (output, output.Extent);
var ui = UIImage.FromImage (cgimage);
imgview.Image = ui;
}
/// <summary>
/// Example using a filter
/// </summary>
public static void VortexDistortion(UITextView display, UIImageView imgview)
{
var uiimage = UIImage.FromFile ("photo.JPG");
var ciimage = new CIImage (uiimage);
var lightTunnel = CIFilter.FromName ("CIVortexDistortion");
lightTunnel [CIFilterInputKey.Image] = ciimage;
lightTunnel [new NSString ("inputCenter")] = new CIVector(300, 400);
lightTunnel [new NSString ("inputAngle")] = new NSNumber(55);
lightTunnel [new NSString ("inputRadius")] = new NSNumber(400);
var output = lightTunnel.OutputImage;
var context = CIContext.FromOptions(null);
var cgimage = context.CreateCGImage (output, output.Extent);
var ui = UIImage.FromImage (cgimage);
imgview.Image = ui;
}
/// <summary>
/// Example using a filter
/// </summary>
public static void Sharpen(UITextView display, UIImageView imgview)
{
var uiimage = UIImage.FromFile ("photo.JPG");
var ciimage = new CIImage (uiimage);
var prespectiveTransform = CIFilter.FromName ("CISharpenLuminance");
prespectiveTransform [CIFilterInputKey.Image] = ciimage;
prespectiveTransform [new NSString ("inputSharpness")] = new NSNumber(0.40);
var output = prespectiveTransform.OutputImage;
var context = CIContext.FromOptions(null);
var cgimage = context.CreateCGImage (output, output.Extent);
var ui = UIImage.FromImage (cgimage);
imgview.Image = ui;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment