Saving image after drawing markup on image

NeelioNeelio AUMember

Hi,

I have a image controller where I displaying an image and allowing the user to draw on the image. I have been able implement the drawing functionality but can't seem to figure out how to save the new image(with drawing) as a image. Please note that the code below is not a complete code and I have not yet implemented the code to save the edited image to file. Before I tried doing so, I wanted to make sure that I'm going about it the correct way.

public partial class LargeImageController : UIViewController
    {
        public UIImage image { get; set; }


        public LargeImageController(IntPtr handle) : base(handle)
        {

        }
        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();          
        }
        public override void LoadView()
        {

            Console.WriteLine("LoadView() Called");
            NavigationItem.SetRightBarButtonItem(
            new UIBarButtonItem(UIBarButtonSystemItem.Edit, (sender, args) => {

                View = new View();
                View.BackgroundColor = UIColor.FromPatternImage(image);
                NavigationItem.SetRightBarButtonItem(
            new UIBarButtonItem(UIBarButtonSystemItem.Save, (s, a) =>
            {
                UIImage saveImage = UIGraphics.GetImageFromCurrentImageContext();


               // System.Diagnostics.Debug.Assert(saveImage != null, "Image is null");
                View.BackgroundColor = UIColor.FromPatternImage(saveImage);


            })
            , true);
            })
            , true);
            base.LoadView();
            //image = UIImage.LoadFromData(NSData.FromUrl(new NSUrl(_fieldNoteImages.Field_Notes_Picture)));
            View = new UIImageView();
            View.BackgroundColor = UIColor.FromPatternImage(image);
        }
        public override void ViewDidLoad()
        {
            Title = "Image";
            base.ViewDidLoad();
        }

    }

    public class View : UIView
    {
        CGPath path;
        CGPoint initialPoint;
        CGPoint latestPoint;
        CGPath pathtotal = new CGPath();

        public override void TouchesBegan(NSSet touches, UIEvent evt)
        {

            base.TouchesBegan(touches, evt);

            UITouch touch = touches.AnyObject as UITouch;
             path= new CGPath();
            if (touch != null)
            {
                initialPoint = touch.LocationInView(this);
            }
        }

        public override void TouchesMoved(NSSet touches, UIEvent evt)
        {

            base.TouchesMoved(touches, evt);

            UITouch touch = touches.AnyObject as UITouch;

            if (touch != null)
            {
                latestPoint = touch.LocationInView(this);
                InvokeOnMainThread(() =>
                {
                    SetNeedsDisplay();
                });
            }
        }
        public override void Draw(CGRect rect)
        {

            base.Draw(rect);

            if (!initialPoint.IsEmpty)
            {

                //get graphics context
                using (CGContext g = UIGraphics.GetCurrentContext())
                {
                    g.SetAllowsAntialiasing(true);
                    //set up drawing attributes
                    g.SetLineWidth(4);
                    UIColor.Red.SetColor();

                    //add lines to the touch points
                    if (path.IsEmpty)
                    {
                        path.AddLines(new CGPoint[] { initialPoint, latestPoint });
                        //initialPoint = latestPoint;
                    }
                    else
                    {

                        //path.AddLines(new CGPoint[] { initialPoint, latestPoint });
                        path.AddLineToPoint(latestPoint);
                    }

                    //use a dashed line
                    g.SetLineDash(0, new nfloat[] {0});
                    pathtotal.AddPath(path);
                    //add geometry to graphics context and draw it
                    g.AddPath(pathtotal);
                    g.DrawPath(CGPathDrawingMode.Stroke);

                }

            }
        }

    }

Regards,
Neel

Sign In or Register to comment.