Skip to content

Instantly share code, notes, and snippets.

@topgenorth
Created May 1, 2014 02:07
Show Gist options
  • Save topgenorth/86af5c37d6bdc71da367 to your computer and use it in GitHub Desktop.
Save topgenorth/86af5c37d6bdc71da367 to your computer and use it in GitHub Desktop.
Draw Image on CGContext
void DrawTargetImage(CGContext context, RectangleF rect)
{
context.ScaleCTM(1, -1);
context.TranslateCTM(0, -Bounds.Height);
context.DrawImage(rect, _targetImage.CGImage);
}
@lobrien
Copy link

lobrien commented May 1, 2014

'
public override void Draw (RectangleF rect)
{
base.Draw (rect);
using (var ctxt = UIGraphics.GetCurrentContext ()) {
ctxt.SetStrokeColor (UIColor.Black.CGColor);
ctxt.ScaleCTM (1, -1);
ctxt.TranslateCTM (0, -Bounds.Height);
ctxt.AddEllipseInRect (new RectangleF (0, 0, 200, 200));

            Func<PointF,PointF> offset = (PointF pt) => new PointF (pt.X - 5, pt.Y - 5);

                 //Unsatisfactory solution:
            if (touchPoint.X != 0 && touchPoint.Y != 0) {
                var old = ctxt.GetCTM ();
                ctxt.ScaleCTM (1, -1);
                ctxt.TranslateCTM (0, -Bounds.Height);
                ctxt.AddEllipseInRect (new RectangleF (offset (touchPoint), new SizeF (10, 10)));
            }
            ctxt.StrokePath ();
        }
    }

    PointF touchPoint;

    public override void TouchesBegan (NSSet touches, UIEvent evt)
    {
        base.TouchesBegan (touches, evt);
        var touch = touches.FirstOrDefault () as UITouch;
        touchPoint = touch.LocationInView (this);

        this.SetNeedsDisplay ();
    }

'''

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