/// <summary> /// Loads the image asynchronous. /// </summary> /// <param name="position">The position.</param> /// <param name="url">The URL.</param> /// <returns>Task.</returns> private Task LoadImageAsync(nint position, string url) { return(Task.Run( () => { var img = GestureHelpers.LoadFromUrl(url); InvokeOnMainThread( () => { var imgView = _scroller.Subviews[position] as UIImageView; if (_pageControl.CurrentPage == position && FadeImages) { FadeImageViewIn(imgView, img); } else { imgView.Image = img; } }); })); }
/// <summary> /// Adds the image. /// </summary> /// <param name="rect">The rect.</paramAddImage> /// <param name="position">The position.</param> /// <param name="im">The im.</param> private void AddImage(CGRect rect, nint position, string im) { var img = new UIImage(); var isRemote = GestureHelpers.IsValidUrl(im); if (isRemote) { //dont await , fire and forget LoadImageAsync(position, im); } else { var fileName = im; if (NSFileManager.DefaultManager.FileExists(fileName)) { var url = new NSUrl(fileName, true); using (var data = NSData.FromUrl(url)) { img = UIImage.LoadFromData(data); } } //img = UIImage.FromFile(im); } UIImageView imgView; nfloat x; nfloat y; nfloat widht; nfloat height; try { imgView = new UIImageView(img) { AutoresizingMask = UIViewAutoresizing.All, ContentMode = UIViewContentMode.ScaleToFill, MultipleTouchEnabled = true, //add 16-12-09 UserInteractionEnabled = true //add 16-12-09 }; if (FadeImages) { imgView.Alpha = 0; } //if first image is local, fade it in if (position == 0 && !isRemote) { FadeImageViewIn(imgView); } //imgView.Frame = new CGRect(rect.Width * position, rect.Top, rect.Width, rect.Height); if (img.Size.Height > img.Size.Width) { widht = img.Size.Width * rect.Height / img.Size.Height; height = rect.Height; x = (rect.Width - widht) / 2; y = 0; } else { widht = rect.Width; height = img.Size.Height * rect.Width / img.Size.Width; x = 0; y = (rect.Height - height) / 2; } imgView.Frame = new CGRect(x, y, widht, height); //add 16-12-09 imgView.AddGestureRecognizer(new UIPinchGestureRecognizer((UIPinchGestureRecognizer obj) => { if (obj.State == UIGestureRecognizerState.Began || obj.State == UIGestureRecognizerState.Changed) { obj.View.Transform *= CGAffineTransform.MakeScale(obj.Scale, obj.Scale); obj.Scale = 1; } })); imgView.AddGestureRecognizer(new UIPanGestureRecognizer((UIPanGestureRecognizer gestureRecognizer) => { AdjustAnchorPointForGestureRecognizer(gestureRecognizer); var image = gestureRecognizer.View; if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed) { var translation = gestureRecognizer.TranslationInView(this); image.Center = new CGPoint(image.Center.X + translation.X, image.Center.Y + translation.Y); // Reset the gesture recognizer's translation to {0, 0} - the next callback will get a delta from the current position. gestureRecognizer.SetTranslation(CGPoint.Empty, image); } })); _scroller.AddSubview(imgView); } catch (Exception) { } }