Sunday, June 9, 2013

Sepia Tone UIViewController Xamarin.IOS.



I made the slider in XCode resolve as "TheSlider"
I made the UIImageView resolve as "TheImage"
I made the Buttons resolve as their text values.

Below is the Code:

public partial class SepiaToneController : UIViewController
    {
        static bool UserInterfaceIdiomIsPhone {
     get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
        }
        string path;
        MonoTouch.CoreImage.CISepiaTone filter = new MonoTouch.CoreImage.CISepiaTone();
        public SepiaToneController (string Path)
            : base (UserInterfaceIdiomIsPhone ? "SepiaToneController_iPhone" : "SepiaToneController_iPad", null)
        {
            path = Path;

        }

        public override void DidReceiveMemoryWarning ()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning ();
            
            // Release any cached data, images, etc that aren't in use.
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            this.Title = "Sepia Tone";
            TheSlider.MinValue = 0;
            TheSlider.MaxValue = 1;
            TheSlider.Value = 0;
        
            Preview.TouchUpInside += delegate {
                UpdateImage();
            };
            Save.TouchUpInside += delegate {
                SaveImage();
            };

    
        }
        public void UpdateImage()
        {
            BTProgressHUD.Show ("Applying Filter. . .", -1, BTProgressHUD.MaskType.Black);
            BTProgressHUD.InvokeInBackground (delegate { 
                
            this.TheImage.ContentMode = UIViewContentMode.ScaleAspectFit;

            if (TheSlider.Value == 0) {
                var image = UIImage.FromFile (path);
                filter.Intensity = TheSlider.Value;
                filter.Image = new CIImage (UIImage.FromFile(path));
                UIImage blah = new UIImage (new CIImage(UIImage.FromFile(path)),1,image.Orientation);
                this.TheImage.Image = blah;


            } else {

                var image = UIImage.FromFile (path);
                filter.Intensity = TheSlider.Value;
                filter.Image = new CIImage (UIImage.FromFile(path));
                var newimage = filter.OutputImage;
                UIImage returnImage = new UIImage(newimage);
                UIImage blah = new UIImage (filter.OutputImage,1,image.Orientation);

                //.ImageByCroppingToRect(new RectangleF(0, 0, this.View.Bounds.Width, this.View.Bounds.Height * 9 / 16)

                this.TheImage.Image = blah;
            
            }
                BTProgressHUD.Dismiss();
            });
        }
        public void SaveImage()
        {
            BTProgressHUD.Show ("Applying Filter. . .", -1, BTProgressHUD.MaskType.Black);
            BTProgressHUD.InvokeInBackground (delegate { 

            var temp = UIImage.FromFile(path);
            filter.Intensity = TheSlider.Value;
            filter.Image = new CIImage (UIImage.FromFile(path));
            CIImage meh = filter.OutputImage;
            UIImage blah = new UIImage(meh,1,temp.Orientation);
            SizeF newSize = new SizeF(UIImage.FromFile(path).Size.Width,UIImage.FromFile(path).Size.Height);
            UIGraphics.BeginImageContextWithOptions(size:newSize, opaque:false, scale:0.0f);
            blah.Draw (new RectangleF(0,0,blah.Size.Width,blah.Size.Height));
            UIImage croppedImage = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();

            NSData data = croppedImage.AsJPEG();
            byte[] dataBytes = new byte[data.Length];
            if(!Directory.Exists(LocationHelper.FilterLocation()))Directory.CreateDirectory(LocationHelper.FilterLocation ());
            System.Runtime.InteropServices.Marshal.Copy(data.Bytes, dataBytes, 0, Convert.ToInt32(data.Length));
            FileStream f = File.OpenWrite(LocationHelper.FilterLocation()+Path.GetFileName(path));
            f.Write(dataBytes,0,dataBytes.Length);
            f.Close();
                BTProgressHUD.Dismiss();
            });
        }
    }


My end product :