private CropImageView CreateImageView(CropView page) { var cropImageView = new CropImageView(Context) { LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent), }; cropImageView.SetImageBitmap(BitmapFactory.DecodeByteArray(page.Image, 0, page.Image.Length)); return(cropImageView); }
public override void ViewDidLoad() { base.ViewDidLoad(); if (!(Element is CropView page)) { return; } _page = page; _image = page.Image; IsCropped = page.IsCropped; }
public override void ViewDidLoad() { base.ViewDidLoad(); var rotateTap = new UITapGestureRecognizer(RotateTap); rotate.AddGestureRecognizer(rotateTap); var zoomTap = new UITapGestureRecognizer(ZoomTap); resize.AddGestureRecognizer(zoomTap); var multiselectTap = new UITapGestureRecognizer(MultiSelectTap); multiSelect.AddGestureRecognizer(multiselectTap); bottomArrow.Transform = CGAffineTransform.MakeRotation((float)(Math.PI)); source = new PhotoCollectionViewSource(); photoCollection.Source = source; photoCollection.RegisterClassForCell(typeof(PhotoCollectionViewCell), nameof(PhotoCollectionViewCell)); photoCollection.SetCollectionViewLayout(new UICollectionViewFlowLayout() { ItemSize = Constants.CellSize, MinimumLineSpacing = 1, MinimumInteritemSpacing = 1, }, false); delegateP = new PhotoCollectionViewFlowDelegate(source); photoCollection.Delegate = delegateP; delegateP.CellClicked += CellAction; _cropView = new CropView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Width)); _cropView.ZoomingStarted += (object sender, UIScrollViewZoomingEventArgs e) => { NavigationItem.RightBarButtonItem.Enabled = false; }; _cropView.ZoomingEnded += (object sender, ZoomingEndedEventArgs e) => { NavigationItem.RightBarButtonItem.Enabled = true; }; cropBackgroundView.BackgroundColor = Constants.R245G245B245; cropBackgroundView.AddSubview(_cropView); NavigationController.NavigationBar.Translucent = false; SetBackButton(); }
void MoveCropView(PointF delta) { // update the crop view by how much it should be moved float xPos = CropView.GetX( ) + delta.X; float yPos = CropView.GetY( ) + delta.Y; // clamp to valid bounds xPos = Math.Max(CropViewMinPos.X, Math.Min(xPos, CropViewMaxPos.X)); yPos = Math.Max(CropViewMinPos.Y, Math.Min(yPos, CropViewMaxPos.Y)); CropView.SetX(xPos); CropView.SetY(yPos); MaskLayer.Position = new PointF(CropView.GetX( ), CropView.GetY( )); }
public void Crop() { using (var form = new CropView(new Bitmap(this.model.Image))) { var result = form.ShowDialog(); if (result == DialogResult.OK) { this.undoRedo.PushToUndoStack((Bitmap)this.model.Image.Clone()); this.undoRedo.ClearRedoStack(); this.model.Image = (Bitmap)form.Image; this.view.Image = this.model.Image; this.view.Redraw(); } } }
private static Button CreateCancelButton(CropView page, string buttonText) { var rotateButton = new Button { Text = buttonText, Style = Application.Current.Resources["DangerButton"] as Style, Margin = ButtonMargin }; rotateButton.Clicked += (sender, ex) => { page.IsCropped = false; page.Navigation.PopModalAsync(); }; return(rotateButton); }
protected virtual void SetImage() { if (ImageAssets.Count == 1) { if (!_isFromCamera) { SetupPhoto(_cellSize); photoView.Image = ImageAssets[0].Item2; } else { SetupPhoto(new CGSize(UIScreen.MainScreen.Bounds.Width - 15 * 2, UIScreen.MainScreen.Bounds.Width - 15 * 2)); _cropView = new CropView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width - 15 * 2, UIScreen.MainScreen.Bounds.Width - 15 * 2)); photoView.AddSubview(_cropView); _resizeButton = new UIImageView(); _resizeButton.Image = UIImage.FromBundle("ic_resize"); _resizeButton.UserInteractionEnabled = true; mainScroll.AddSubview(_resizeButton); _resizeButton.AutoPinEdge(ALEdge.Left, ALEdge.Left, photoView, 15f); _resizeButton.AutoPinEdge(ALEdge.Bottom, ALEdge.Bottom, photoView, -15f); _rotateButton = new UIImageView(); _rotateButton.Image = UIImage.FromBundle("ic_rotate"); _rotateButton.UserInteractionEnabled = true; mainScroll.AddSubview(_rotateButton); _rotateButton.AutoPinEdge(ALEdge.Left, ALEdge.Right, _resizeButton, 15f); _rotateButton.AutoPinEdge(ALEdge.Bottom, ALEdge.Bottom, photoView, -15f); var rotateTap = new UITapGestureRecognizer(RotateTap); _rotateButton.AddGestureRecognizer(rotateTap); var zoomTap = new UITapGestureRecognizer(ZoomTap); _resizeButton.AddGestureRecognizer(zoomTap); } } else { SetupPhotoCollection(); var galleryCollectionViewSource = new PhotoGalleryViewSource(ImageAssets); photoCollection.Source = galleryCollectionViewSource; photoCollection.BackgroundColor = UIColor.White; } }
private static Button CreateOkButton(CropView page, CropImageView view, string buttonText) { var button = new Button { Text = buttonText, Style = Application.Current.Resources["ActionButton"] as Style, Margin = ButtonMargin }; button.Clicked += (sender, ex) => { var cropped = view.CroppedImage; using (var memory = new MemoryStream()) { cropped.Compress(Bitmap.CompressFormat.Jpeg, 100, memory); page.CroppedImage = memory.ToArray(); } page.IsCropped = true; page.Navigation.PopModalAsync(); }; return(button); }
public CropViewDelegate(UIViewController parent, CropView page) { _parent = parent; _page = page; }
public override void ViewDidLoad() { base.ViewDidLoad(); var rotateTap = new UITapGestureRecognizer(RotateTap); rotate.AddGestureRecognizer(rotateTap); var zoomTap = new UITapGestureRecognizer(ZoomTap); resize.AddGestureRecognizer(zoomTap); var multiselectTap = new UITapGestureRecognizer(MultiSelectTap); multiSelect.AddGestureRecognizer(multiselectTap); bottomArrow.Transform = CGAffineTransform.MakeRotation((float)(Math.PI)); source = new PhotoCollectionViewSource(_m); photoCollection.Source = source; photoCollection.RegisterClassForCell(typeof(PhotoCollectionViewCell), nameof(PhotoCollectionViewCell)); photoCollection.SetCollectionViewLayout(new UICollectionViewFlowLayout() { ItemSize = Constants.CellSize, MinimumLineSpacing = 1, MinimumInteritemSpacing = 1, }, false); delegateP = new PhotoCollectionViewFlowDelegate(source); photoCollection.Delegate = delegateP; delegateP.CellClicked += CellAction; _cropView = new CropView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Width)); _cropView.ZoomingStarted += (object sender, UIScrollViewZoomingEventArgs e) => { NavigationItem.RightBarButtonItem.Enabled = false; }; _cropView.ZoomingEnded += (object sender, ZoomingEndedEventArgs e) => { NavigationItem.RightBarButtonItem.Enabled = true; }; var albums = new List <PHAssetCollection>(); var sortedAlbums = new List <Tuple <string, PHFetchResult> >(); var fetchOptions = new PHFetchOptions(); var allAlbums = PHAssetCollection.FetchAssetCollections(PHAssetCollectionType.Album, PHAssetCollectionSubtype.AlbumRegular, null) .Cast <PHAssetCollection>(); albums.AddRange(allAlbums); var smartAlbums = PHAssetCollection.FetchAssetCollections(PHAssetCollectionType.SmartAlbum, PHAssetCollectionSubtype.AlbumRegular, null) .Cast <PHAssetCollection>(); albums.AddRange(smartAlbums); fetchOptions.Predicate = NSPredicate.FromFormat("mediaType == %d", FromObject(PHAssetMediaType.Image)); foreach (var item in albums) { var firstAsset = PHAsset.FetchAssets(item, fetchOptions); if (firstAsset.Count > 0) { sortedAlbums.Add(new Tuple <string, PHFetchResult>(item.LocalizedTitle, firstAsset)); } } sortedAlbums = sortedAlbums.OrderByDescending(a => a.Item2.Count).ToList(); _modalFolderView.BackgroundColor = UIColor.White; var folderTable = new UITableView(); folderTable.Bounces = false; folderTable.AllowsSelection = false; folderTable.RowHeight = 90; folderTable.SeparatorStyle = UITableViewCellSeparatorStyle.None; var folderSource = new FolderTableViewSource(sortedAlbums); folderSource.CellAction += (ActionType arg1, Tuple <string, PHFetchResult> arg2) => { TitleTapped(); _titleLabel.Text = arg2.Item1; source.UpdateFetchResult(arg2.Item2); photoCollection.SetContentOffset(new CGPoint(0, 0), false); photoCollection.ReloadData(); if (!source.MultiPickMode) { delegateP.ItemSelected(photoCollection, NSIndexPath.FromItemSection(0, 0)); } }; folderTable.Source = folderSource; _modalFolderView.AddSubview(folderTable); folderTable.RegisterClassForCellReuse(typeof(AlbumTableViewCell), nameof(AlbumTableViewCell)); folderTable.AutoPinEdgesToSuperviewEdges(); folderTable.ReloadData(); cropBackgroundView.BackgroundColor = Constants.R245G245B245; cropBackgroundView.AddSubview(_cropView); NavigationController.NavigationBar.Translucent = false; SetBackButton(); _titleLabel.Text = sortedAlbums.FirstOrDefault()?.Item1; source.UpdateFetchResult(sortedAlbums.FirstOrDefault()?.Item2); }
void SetMode(CropMode mode) { if (mode == Mode) { throw new Exception(string.Format("Crop Mode {0} requested, but already in that mode.", mode)); } switch (mode) { case CropMode.Editing: { // If we're entering edit mode for the first time if (Mode == CropMode.None) { // Animate in the mask SimpleAnimator_Float floatAnimator = new SimpleAnimator_Float(MaskLayer.Opacity, MaskFadeAmount, MaskFadeTime, delegate(float percent, object value) { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { MaskLayer.Opacity = (float)value; }); }, null); floatAnimator.Start( ); // turn on our cropper CropView.Visibility = ViewStates.Visible; // and set the source image to the scaled source. ImageView.SetImageBitmap(ScaledSourceImage); } // else we're coming FROM Preview Mode, so we need to animate else { Animating = true; // setup the dimension changes System.Drawing.SizeF startSize = new System.Drawing.SizeF(ImageView.Width, ImageView.Height); System.Drawing.SizeF endSize = new System.Drawing.SizeF(CropView.Width, CropView.Height); PointF startPos = new PointF(ImageView.GetX( ), ImageView.GetY( )); PointF endPos = new PointF(CropView.GetX( ), CropView.GetY( )); // now animate the cropped image up to its full size AnimateImageView(ImageView, startPos, endPos, startSize, endSize, ImageAnimationTime, delegate { ImageView.SetImageBitmap(null); // release any cropped image we had. if (CroppedImage != null) { CroppedImage.Dispose( ); CroppedImage = null; } // release the scaled version if we had it if (ScaledCroppedImage != null) { ScaledCroppedImage.Dispose( ); ScaledCroppedImage = null; } ImageView.SetImageBitmap(ScaledSourceImage); ImageView.LayoutParameters.Width = ScaledSourceImage.Width; ImageView.LayoutParameters.Height = ScaledSourceImage.Height; // center the image ImageView.SetX((ScreenSize.Width - ImageView.LayoutParameters.Width) / 2); ImageView.SetY((ScreenSize.Height - ImageView.LayoutParameters.Height) / 2); MaskLayer.Visibility = ViewStates.Visible; CropView.Visibility = ViewStates.Visible; SimpleAnimator_Float floatAnimator = new SimpleAnimator_Float(MaskLayer.Opacity, MaskFadeAmount, MaskFadeTime, delegate(float percent, object value) { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { MaskLayer.Opacity = (float)value; CropView.Alpha = percent; }); }, // FINISHED MASK FADE-OUT delegate { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { Animating = false; }); }); floatAnimator.Start( ); }); } break; } case CropMode.Previewing: { // don't allow a state change while we're animating Animating = true; SimpleAnimator_Float floatAnimator = new SimpleAnimator_Float(MaskLayer.Opacity, 1.00f, MaskFadeTime, delegate(float percent, object value) { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { MaskLayer.Opacity = (float)value; CropView.Alpha = 1.0f - percent; }); }, // FINISHED MASK FADE-IN delegate { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { // hide the mask and cropper MaskLayer.Visibility = ViewStates.Gone; CropView.Visibility = ViewStates.Gone; // create the cropped image CroppedImage = CropImage(SourceImage, new System.Drawing.RectangleF(CropView.GetX( ) - CropViewMinPos.X, CropView.GetY( ) - CropViewMinPos.Y, CropView.LayoutParameters.Width, CropView.LayoutParameters.Height)); // create a scaled version of the cropped image float scaledWidth = (float)CroppedImage.Width * (1.0f / ScreenToImageScalar); float scaledHeight = (float)CroppedImage.Height * (1.0f / ScreenToImageScalar); ScaledCroppedImage = Bitmap.CreateScaledBitmap(CroppedImage, (int)scaledWidth, (int)scaledHeight, false); // set the scaled cropped image ImageView.SetImageBitmap(null); ImageView.SetImageBitmap(ScaledCroppedImage); // start the scaled cropped image scaled down further to match its size within the full image. ImageView.SetX(CropView.GetX( )); ImageView.SetY(CropView.GetY( )); ImageView.LayoutParameters.Width = CropView.Width; ImageView.LayoutParameters.Height = CropView.Height; // setup the dimension changes System.Drawing.SizeF startSize = new System.Drawing.SizeF(ScaledCroppedImage.Width, ScaledCroppedImage.Height); System.Drawing.SizeF endSize; if (ScreenSize.Width < ScreenSize.Height) { endSize = new System.Drawing.SizeF(ScreenSize.Width, (float)System.Math.Ceiling(ScreenSize.Width * (startSize.Width / startSize.Height))); } else { endSize = new System.Drawing.SizeF((float)System.Math.Ceiling(ScreenSize.Height * (startSize.Height / startSize.Width)), ScreenSize.Height); } PointF startPos = new PointF(CropView.GetX( ), CropView.GetY( )); PointF endPos = new PointF((ScreenSize.Width - endSize.Width) / 2, (ScreenSize.Height - endSize.Height) / 2); // now animate the cropped image up to its full size AnimateImageView(ImageView, startPos, endPos, startSize, endSize, ImageAnimationTime, delegate { Animating = false; }); }); }); floatAnimator.Start( ); break; } } Mode = mode; }