Exemple #1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            screenSize                    = UIScreen.MainScreen.Bounds.Size;
            mainContainer                 = new UIView();
            mainContainer.Frame           = new CGRect(0, 0, screenSize.Width, screenSize.Height);
            mainContainer.BackgroundColor = UIColor.Black;
            this.View.AddSubview(mainContainer);

            double maxWidth  = screenSize.Width;
            double maxHeight = screenSize.Height;

            // Bottom Bar, which will contain the controlls
            UIView bottomBar = new UIView();

            bottomBar.Frame           = new CGRect(0, screenSize.Height - 70, screenSize.Width, 50);
            bottomBar.BackgroundColor = UIColor.Black;
            mainContainer.AddSubview(bottomBar);

            // Adding Items to bottom bar
            var btnCancel = new UIButton(new CGRect(30, 10, 100, 30));

            btnCancel.SetTitle("Cancel", UIControlState.Normal);
            btnCancel.SetTitleColor(UIColor.White, UIControlState.Normal);
            btnCancel.BackgroundColor = UIColor.Clear;
            btnCancel.TouchUpInside  += async(s, e) =>
            {
                await this.DismissViewControllerAsync(true);

                _callback?.Invoke(false);
            };
            bottomBar.AddSubview(btnCancel);

            UIButton rotationButton = new UIButton();

            image = new UIImage("rotate_button.png");
            rotationButton.Frame = new CGRect(screenSize.Width / 2 - 40, 10, 80, 30);
            rotationButton.SetImage(image, UIControlState.Normal);
            rotationButton.AddTarget((s, e) => { RotateImageTapped(); }, UIControlEvent.TouchUpInside);
            bottomBar.AddSubview(rotationButton);


            var btnSave = new UIButton(new CGRect(screenSize.Width - 120, 10, 100, 30));

            btnSave.SetTitle("Save", UIControlState.Normal);
            btnSave.SetTitleColor(UIColor.White, UIControlState.Normal);
            btnSave.BackgroundColor = UIColor.Clear;
            btnSave.TouchUpInside  += Crop;
            bottomBar.AddSubview(btnSave);

            if (ImagePath == null)
            {
                return;
            }
            using (var image = UIImage.FromFile(ImagePath))
            {
                imageWidth          = Math.Min(image.Size.Width, maxWidth);
                imageHeight         = imageWidth * image.Size.Height / image.Size.Width;
                mainImageView       = new UIImageView(new CGRect(0, ((screenSize.Height - 70) - imageHeight) / 2, imageWidth, imageHeight));
                mainImageView.Image = image;
                xRatio = image.Size.Width / imageWidth;
                yRatio = image.Size.Height / imageHeight;
            }
            double cropW = Math.Min(imageWidth, imageHeight) > 300 ? 300 : 200;

            cropperView = new CropperView((imageWidth - cropW) / 2, (imageHeight - cropW) / 2, cropW, cropW)
            {
                Frame = mainImageView.Frame
            };
            mainContainer.AddSubviews(mainImageView, cropperView);

            nfloat dx = 0;
            nfloat dy = 0;

            pan = new UIPanGestureRecognizer(() =>
            {
                if ((pan.State == UIGestureRecognizerState.Began || pan.State == UIGestureRecognizerState.Changed) && (pan.NumberOfTouches == 1))
                {
                    var p0 = pan.LocationInView(View);

                    if (dx == 0)
                    {
                        dx = p0.X - cropperView.Origin.X;
                    }

                    if (dy == 0)
                    {
                        dy = p0.Y - cropperView.Origin.Y;
                    }

                    var p1             = new CGPoint(p0.X - dx, p0.Y - dy);
                    cropperView.Origin = p1;
                }
                else if (pan.State == UIGestureRecognizerState.Ended)
                {
                    dx = 0;
                    dy = 0;
                }
            });

            nfloat s0 = 1;

            pinch = new UIPinchGestureRecognizer(() =>
            {
                nfloat s         = pinch.Scale;
                nfloat ds        = (nfloat)Math.Abs(s - s0);
                nfloat sf        = 0;
                const float rate = 0.5f;

                if (s >= s0)
                {
                    sf = 1 + ds * rate;
                }
                else if (s < s0)
                {
                    sf = 1 - ds * rate;
                }
                s0 = s;

                cropperView.CropSize = new CGSize(cropperView.CropSize.Width * sf, cropperView.CropSize.Height * sf);

                if (pinch.State == UIGestureRecognizerState.Ended)
                {
                    s0 = 1;
                }
            });

            cropperView.AddGestureRecognizer(pan);
            cropperView.AddGestureRecognizer(pinch);
        }
Exemple #2
0
        public void ReLoadView(bool roate)
        {
            cropperView = new CropperView((imageHeight - 200) / 2, (imageWidth - 200) / 2, 200, 200)
            {
                Frame = mainImageView.Frame
            };
            mainContainer.AddSubviews(mainImageView, cropperView);
            nfloat dx = 0;
            nfloat dy = 0;

            pan = new UIPanGestureRecognizer(() =>
            {
                if ((pan.State == UIGestureRecognizerState.Began || pan.State == UIGestureRecognizerState.Changed) && (pan.NumberOfTouches == 1))
                {
                    var p0 = pan.LocationInView(View);

                    if (dx == 0)
                    {
                        dx = p0.X - cropperView.Origin.X;
                    }

                    if (dy == 0)
                    {
                        dy = p0.Y - cropperView.Origin.Y;
                    }

                    var p1             = new CGPoint(p0.X - dx, p0.Y - dy);
                    cropperView.Origin = p1;
                }
                else if (pan.State == UIGestureRecognizerState.Ended)
                {
                    dx = 0;
                    dy = 0;
                }
            });

            nfloat s0 = 1;

            pinch = new UIPinchGestureRecognizer(() =>
            {
                nfloat s         = pinch.Scale;
                nfloat ds        = (nfloat)Math.Abs(s - s0);
                nfloat sf        = 0;
                const float rate = 0.5f;

                if (s >= s0)
                {
                    sf = 1 + ds * rate;
                }
                else if (s < s0)
                {
                    sf = 1 - ds * rate;
                }
                s0 = s;

                cropperView.CropSize = new CGSize(cropperView.CropSize.Width * sf, cropperView.CropSize.Height * sf);

                if (pinch.State == UIGestureRecognizerState.Ended)
                {
                    s0 = 1;
                }
            });

            cropperView.AddGestureRecognizer(pan);
            cropperView.AddGestureRecognizer(pinch);
        }