public override void DidCropImageToRect(TOCropViewController cropViewController, CGRect cropRect, nint angle) { cropViewController.PresentingViewController.DismissViewController(true, null); TOCropViewController owner; _tcs.SetResult(_owner.TryGetTarget(out owner) ? cropViewController.FinalImage.UIImageToBytes() : null); }
public override void ViewDidAppear(bool animated) { base.ViewDidAppear(animated); try { if (_isShown) { return; } _isShown = true; var image = new UIImage(NSData.FromArray(_image)); _image = null; _selector = new CropViewDelegate(this, _page); var picker = new TOCropViewController(image) { Delegate = _selector }; PresentViewController(picker, false, null); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
void Button_TouchUpInside(object sender, EventArgs e) { var cropVC = new TOCropViewController(TOCropViewCroppingStyle.Default, ImageView.Image); cropVC.Delegate = new CropVCDelegate(this); this.PresentViewController(cropVC, true, null); }
public override void ViewDidAppear(bool animated) { base.ViewDidAppear(animated); try { if (!IsShown) { IsShown = true; UIImage image = new UIImage(NSData.FromArray(Image)); Image = null; selector = new CropViewDelegate(this); TOCropViewController picker = new TOCropViewController(image); // Demo for Circular Cropped Image //TOCropViewController picker = new TOCropViewController(TOCropViewCroppingStyle.Circular, image); picker.Delegate = selector; PresentViewController(picker, false, null); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
public override void DidCropToImage(TOCropViewController cropViewController, UIImage image, CoreGraphics.CGRect cropRect, nint angle) { DidCrop = true; try { if (image != null) { CroppedImage = image.AsPNG().ToArray(); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { if (image != null) { image.Dispose(); image = null; } } parent.DismissViewController(true, () => { Xamarin.Forms.Application.Current.MainPage.Navigation.PopModalAsync(); }); }
public override void DidCropToImage(TOCropViewController cropViewController, UIImage image, CoreGraphics.CGRect cropRect, nint angle) { IsCropped = true; try { if (image != null) { _page.CroppedImage = image.AsJPEG().ToArray(); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { if (image != null) { image.Dispose(); image = null; } CloseView(); } }
public override void DidCropToImage(TOCropViewController cropViewController, UIImage image, CoreGraphics.CGRect cropRect, nint angle) { DidCrop = true; try { if (image != null) { App.CroppedImage = image.AsPNG().ToArray(); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { if (image != null) { image.Dispose(); image = null; } } parent.DismissViewController(true, App.PopModal); }
public void ShowFromFile(ImageCropper imageCropper, string imageFile) { UIImage image = UIImage.FromFile(imageFile); TOCropViewController cropViewController; if (imageCropper.CropShape == ImageCropper.CropShapeType.Oval) { cropViewController = new TOCropViewController(TOCropViewCroppingStyle.Circular, image); } else { cropViewController = new TOCropViewController(image); } if (imageCropper.AspectRatioX > 0 && imageCropper.AspectRatioY > 0) { cropViewController.AspectRatioPreset = TOCropViewControllerAspectRatioPreset.Custom; cropViewController.ResetAspectRatioEnabled = false; cropViewController.AspectRatioLockEnabled = true; cropViewController.CustomAspectRatio = new CGSize(imageCropper.AspectRatioX, imageCropper.AspectRatioY); } cropViewController.OnDidCropToRect = (outImage, cropRect, angle) => { Finalize(imageCropper, outImage); }; cropViewController.OnDidCropToCircleImage = (outImage, cropRect, angle) => { Finalize(imageCropper, outImage); }; cropViewController.OnDidFinishCancelled = (cancelled) => { imageCropper.Faiure?.Invoke(); UIApplication.SharedApplication.KeyWindow.RootViewController.DismissViewController(true, null); }; //UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(cropViewController, true, null); // Patch for Modal iOS: https://github.com/stormlion227/ImageCropper.Forms/issues/22 UIWindow window = UIApplication.SharedApplication.KeyWindow; UIViewController viewController = window.RootViewController; while (viewController.PresentedViewController != null) { viewController = viewController.PresentedViewController; } viewController.PresentViewController(cropViewController, true, null); // end on iOS Modal patch if (!string.IsNullOrWhiteSpace(imageCropper.PageTitle) && cropViewController.TitleLabel != null) { UILabel titleLabel = cropViewController.TitleLabel; titleLabel.Text = imageCropper.PageTitle; } }
public override void DidCropImageToRect(TOCropViewController cropViewController, CGRect cropRect, nint angle) { cropViewController.PresentingViewController.DismissViewController(true, null); ViewController owner; if (_owner.TryGetTarget(out owner)) { owner.ImageView.Image = cropViewController.FinalImage; } }
public Task <byte[]> CropImageFromOriginalToBytes(string filePath) { var image = UIImage.FromFile(filePath); var viewController = new TOCropViewController(TOCropViewCroppingStyle.Default, image); var ndelegate = new CropVcDelegate(viewController); viewController.Delegate = ndelegate; viewController.PresentUsingRootViewController(); var result = ndelegate.Task.ContinueWith(t => t).Unwrap(); return(result); }
public void ShowFromFile(ImageCropper imageCropper, string imageFile) { UIImage image = UIImage.FromFile(imageFile); TOCropViewController cropViewController; if (imageCropper.CropShape == ImageCropper.CropShapeType.Oval) { cropViewController = new TOCropViewController(TOCropViewCroppingStyle.Circular, image); } else { cropViewController = new TOCropViewController(image); } cropViewController.DoneButtonTitle = imageCropper.CropButtonTitle; cropViewController.CancelButtonTitle = imageCropper.CancelButtonTitle; if (imageCropper.AspectRatioX > 0 && imageCropper.AspectRatioY > 0) { cropViewController.AspectRatioPreset = TOCropViewControllerAspectRatioPreset.Custom; cropViewController.ResetAspectRatioEnabled = false; cropViewController.AspectRatioLockEnabled = true; cropViewController.CustomAspectRatio = new CGSize(imageCropper.AspectRatioX, imageCropper.AspectRatioY); } cropViewController.OnDidCropToRect = (outImage, cropRect, angle) => { Finalize(imageCropper, outImage); }; cropViewController.OnDidCropToCircleImage = (outImage, cropRect, angle) => { Finalize(imageCropper, outImage); }; cropViewController.OnDidFinishCancelled = (cancelled) => { imageCropper.Faiure?.Invoke(); UIApplication.SharedApplication.KeyWindow.RootViewController.DismissViewController(true, null); }; UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(cropViewController, true, null); if (!string.IsNullOrWhiteSpace(imageCropper.PageTitle) && cropViewController.TitleLabel != null) { UILabel titleLabel = cropViewController.TitleLabel; titleLabel.Text = imageCropper.PageTitle; } }
/// <summary> /// On crop to rectangle complete /// --------------- /// NOTE: /// As of 23/04/2018, the cropping library utilized (TOCropViewController) has the following open issue on GitHub: /// https://github.com/TimOliver/TOCropViewController/issues/239 /// /// Steps to repro: /// 1. Select a non-square image /// 2. Zoom and/or pan on the image /// 3. Click the reset button at the bottom (2nd button from the right) /// 4. Click the done button /// /// Expected: /// cropViewController.FinalImage will be square /// /// Result: /// cropViewController.FinalImage will NOT be square: /// FinalImage Width and FinalImage Height will differ by a pixel (from the repro attempts conducted) /// /// Work around used: /// /// [Let diff = absolute(FinalImage.Width - FinalImage.Height)] /// /// 1. If {diff} > 1 px: throw error pop-up. This case is where the user /// purposely dragged the rectangular image out and then hitting done. /// Return from function. /// /// 2. If 1px >= {diff} > 0px: set the outImageBitmap to a square cropping, /// thus removing the {diff} offset on either the width or height /// --------------- /// </summary> /// <param name="cropVC">TOCropViewController object</param> /// <param name="cropRect">Rectangled cropped</param> /// <param name="angle">Angle at which cropped</param> public override void DidCropImageToRect(TOCropViewController cropViewController, CGRect cropRect, nint angle) { UIImage outImage = cropViewController.FinalImage; SKBitmap outImageBitmap = outImage.ToSKBitmap(); nfloat width = outImage.Size.Width, height = outImage.Size.Height; // Work-around epsilon const float epsilon = 1.0f; // Case 1 (See comment on work-around above) if (Math.Abs(width - height) > epsilon) { // if it's a non-square image var alert = UIAlertController.Create("Cropping Error", "The AI model can only except a 1:1 aspect ratio. Please crop to a square.", UIAlertControllerStyle.Alert); // Add Actions alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); // Present Alert cropViewController.PresentViewController(alert, true, null); return; } else if (width != height) { // Case 2 (See comment on work-around above) SKBitmap image = outImage.ToSKBitmap(); int minDimension = (int)Math.Min(width, height); // Crop to the minimum dimension image.ExtractSubset(outImageBitmap, new SKRectI(0, 0, minDimension, minDimension)); } // Get analysis storyboard UIStoryboard analysisStoryboard = UIStoryboard.FromName("Analysis", null); // Grab analysis view Controller reference AnalysisViewController analysisViewController = analysisStoryboard.InstantiateInitialViewController() as AnalysisViewController; analysisViewController.InputImage = outImage; analysisViewController.ImageUrl = imageUrl; analysisViewController.InputImageBitmap = outImageBitmap; // Push analysis view controller onto stack cropViewController.NavigationController.PushViewController(analysisViewController, true); }
public Task <byte[]> CropImageFromOriginalToBytes(string filePath, CropAspect aspect = CropAspect.Custom) { var image = UIImage.FromFile(filePath); //crop image var viewController = new TOCropViewController(TOCropViewCroppingStyle.Default, image); var ndelegate = new CropVcDelegate(viewController); switch (aspect) { case CropAspect.Square: viewController.AspectRatioLockEnabled = true; viewController.AspectRatioPickerButtonHidden = true; viewController.AspectRatioPreset = TOCropViewControllerAspectRatioPreset.Square; break; } viewController.Delegate = ndelegate; //show viewController.PresentUsingRootViewController(); var result = ndelegate.Task.ContinueWith(t => t).Unwrap(); return(result); }
public override void DidFinishCancelled(TOCropViewController cropViewController, bool cancelled) { parent.DismissViewController(true, () => { Xamarin.Forms.Application.Current.MainPage.Navigation.PopModalAsync(); }); }
public override void DidFinishCancelled(TOCropViewController cropViewController, bool cancelled) { parent.DismissViewController(true, App.PopModal); }
public override void DidFinishCancelled(TOCropViewController cropViewController, bool cancelled) { //dissmiss viewcontroler cropViewController.PresentingViewController.DismissViewController(true, null); _tcs.SetResult(null); }
public CropVcDelegate(TOCropViewController owner) { _owner = new WeakReference <TOCropViewController> (owner); _tcs = new TaskCompletionSource <byte[]> (); }
public override void DidFinishCancelled(TOCropViewController cropViewController, bool cancelled) { CloseView(); }
/*protected override void OnElementChanged(VisualElementChangedEventArgs e) * { * base.OnElementChanged(e); * * var page = e.NewElement as CropView; * var view = NativeView; * * var label = new UILabel(new RectangleF(20, 40, 40, 40)); * label.AdjustsFontSizeToFitWidth = true; * label.TextColor = UIColor.White; * if (page != null) * { * label.Text = "this is test"; * } * * view.Add(label); * } */ public override void ViewDidAppear(bool animated) { base.ViewDidAppear(animated); try { if (!IsShown) { IsShown = true; UIImage image = new UIImage(NSData.FromArray(Image)); Image = null; selector = new CropViewDelegate(this); TOCropViewController picker = new TOCropViewController(TOCropViewCroppingStyle.Default, image); // Demo for Circular Cropped Image //TOCropViewController picker = new TOCropViewController(TOCropViewCroppingStyle.Circular, image); //picker.AspectRatioLockEnabled = false; picker.ChildViewControllerForStatusBarHidden(); picker.AspectRatioPickerButtonHidden = true; picker.RotateButtonsHidden = true; picker.RotateClockwiseButtonHidden = true; picker.AspectRatioLockEnabled = true; picker.ResetAspectRatioEnabled = false; //picker.AspectRatioPreset = TOCropViewControllerAspectRatioPreset.Original; picker.Delegate = selector; //picker.DidRotate(UIInterfaceOrientation.LandscapeRight); //picker.ImageCropFrame = new CoreGraphics.CGRect(picker.ImageCropFrame.X, picker.ImageCropFrame.Y, picker.ImageCropFrame.Width, picker.ImageCropFrame.Height - 400); //picker.Title = "this is test"; //picker.CropView. //var size = picker.CropView.Bounds; //picker.ImageCropFrame = new CoreGraphics.CGRect(size.X, size.Y, size.Width, size.Height - 200); //picker.CropView.LargeContentTitle = "sljakskljslaJSLAJSLA"; //Title = "hakhdsdl"; /*UILabel label = new UILabel(); * label.Frame = new CoreGraphics.CGRect(0, 0, 150, 150); * label.Text = "helloooo"; * label.TextColor = UIColor.Yellow; * UIView view = new UIView(); * view.BackgroundColor = UIColor.Orange; * view.ContentMode = UIViewContentMode.Bottom; * view.Add(label);*/ //picker.Add(view); //UINavigationController nav = new UINavigationController(picker); //nav.Title = "djflkdjlfkdlkf"; //nav.colo //nav.ModalPresentationStyle = UIModalPresentationStyle.Popover;//.BlurOverFullScreen;//.Automatic;//.PageSheet; // Whichever you prefer //nav.NavigationBar.Frame = new CoreGraphics.CGRect(100, 100, 100, 100); //nav.NavigationBar.BackgroundColor = UIColor.Red; //nav.NavigationBar.BarTintColor = UIColor.Yellow; picker.ModalPresentationStyle = UIModalPresentationStyle.Popover; //picker.PreferredStatusBarStyle(); //picker.View = view; //picker.Toolbar = new TOCropToolbar(); PresentViewController(picker, true, null); //UILabel label = picker.TitleLabel; var size = picker.CropView.Bounds; //Heading. UILabel labelHeading = new UILabel(); labelHeading.Frame = new CoreGraphics.CGRect(size.X, size.Height - 210, size.Width, 75); labelHeading.Text = "Crop Image"; labelHeading.Font = UIFont.SystemFontOfSize(30); labelHeading.TextAlignment = UITextAlignment.Center; labelHeading.TextColor = UIColor.White; //Sub Heading. string text = Helper.Helper.SelectedIdentificatorText; if (text == "ID") { text = "ID Card"; } //text = "ID Card"; UILabel labelSubHeading = new UILabel(); labelSubHeading.Frame = new CoreGraphics.CGRect(size.X, size.Height - 200, size.Width, 170); labelSubHeading.Text = $"Use the marker to mark the area around your card. So that we only see your {text}."; labelSubHeading.Font = UIFont.SystemFontOfSize(20); labelSubHeading.TextAlignment = UITextAlignment.Center; labelSubHeading.TextColor = UIColor.White; labelSubHeading.Lines = 3; labelSubHeading.LineBreakMode = UILineBreakMode.WordWrap; UIView view = new UIView(); view.Frame = new CoreGraphics.CGRect(size.X, size.Y, size.Width, 150); view.ContentMode = UIViewContentMode.Bottom; view.Add(labelHeading); view.Add(labelSubHeading); //picker.Title = "this is demo"; picker.Add(view); //PresentViewController(picker, true, null); //picker.CropView.AddSubview(view); picker.CustomAspectRatio = new CoreGraphics.CGSize(600, 400); picker.ModalPresentationStyle = UIModalPresentationStyle.Custom; } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }