Skip to content

Instantly share code, notes, and snippets.

@follesoe
Created March 22, 2011 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save follesoe/882223 to your computer and use it in GitHub Desktop.
Save follesoe/882223 to your computer and use it in GitHub Desktop.
MonoTouch example of cropping an image.
public UIImage Crop(UIImage image, RectangleF section)
{
// Start context.
UIGraphics.BeginImageContext(section.Size);
var ctx = UIGraphics.GetCurrentContext();
// Clear the image with red.
ctx.SetRGBFillColor(255, 255, 255, 255);
ctx.FillRect(new RectangleF(new PointF(0, 0), section.Size));
// Setting transform to flip the image.
var transform = new MonoTouch.CoreGraphics.CGAffineTransform(1, 0, 0, -1, 0, section.Height);
ctx.ConcatCTM(transform);
// Drawing the image.
var drawSource = CreateDrawRectangle(image, section);
ctx.DrawImage(drawSource, image.CGImage.WithImageInRect(section));
// Extracting the image and ending.
var croppedImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return croppedImage;
}
@CyDragon80
Copy link

CyDragon80 commented Jun 8, 2012

Where is CreateDrawRectangle() ? I can't seem to find it.
Update: I believe it is defined in this source file
https://github.com/follesoe/FacebookBigProfile/blob/761ae309df72b12864f6e91115bb2932ab264978/FacebookBigProfile/MainView.xib.cs#L492

@iOSSer
Copy link

iOSSer commented Jul 26, 2013

Where is CreateDrawRectangle() ? I can't seem to find it.

@beeradmoore
Copy link

Where is CreateDrawRectangle() ? I can't seem to find it.

@diego-rzg
Copy link

Where is CreateDrawRectangle() ? I can't seem to find it.

@sreenit
Copy link

sreenit commented Oct 27, 2014

Where is CreateDrawRectangle() ? I can't seem to find it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment