Exemplo n.º 1
0
        void StartLoadingAnimated(bool animated)
        {
            LoadIndicator.StartAnimating();

            UIView.Animate((animated) ? 0.2 : 0.0,
                           () =>
            {
                ImageView.Alpha     = 0f;
                LoadIndicator.Alpha = 1f;
            });
        }
Exemplo n.º 2
0
        public void SetOriginalImage(UIImage originalImage, CGRect cropFrame)
        {
            LoadIndicator.StartAnimating();
            InvokeOnMainThread(() =>
            {
                CGImage imageRef = originalImage.CGImage;
                UIImageOrientation imageOrientation = originalImage.Orientation;

                if (imageRef == null)
                {
                    return;
                }


                var bytesPerRow          = 0;
                var width                = imageRef.Width;
                var height               = imageRef.Height;
                var bitsPerComponent     = imageRef.BitsPerComponent;
                CGColorSpace colorSpace  = CGColorSpace.CreateDeviceRGB();
                CGBitmapFlags bitmapInfo = imageRef.BitmapInfo;

                switch (imageOrientation)
                {
                case UIImageOrientation.RightMirrored:
                case UIImageOrientation.LeftMirrored:
                case UIImageOrientation.Right:
                case UIImageOrientation.Left:
                    width  = imageRef.Height;
                    height = imageRef.Width;
                    break;

                default:
                    break;
                }

                CGSize imageSize        = new CGSize(width, height);
                CGBitmapContext context = new CGBitmapContext(null,
                                                              width,
                                                              height,
                                                              bitsPerComponent,
                                                              bytesPerRow,
                                                              colorSpace,
                                                              bitmapInfo);

                colorSpace.Dispose();

                if (context == null)
                {
                    imageRef.Dispose();
                    return;
                }

                switch (imageOrientation)
                {
                case UIImageOrientation.RightMirrored:
                case UIImageOrientation.Right:
                    context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2);
                    context.RotateCTM(-((nfloat)Math.PI / 2));
                    context.TranslateCTM(-imageSize.Height / 2, -imageSize.Width / 2);
                    break;

                case UIImageOrientation.LeftMirrored:
                case UIImageOrientation.Left:
                    context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2);
                    context.RotateCTM((nfloat)(Math.PI / 2));
                    context.TranslateCTM(-imageSize.Height / 2, -imageSize.Width / 2);
                    break;

                case UIImageOrientation.Down:
                case UIImageOrientation.DownMirrored:
                    context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2);
                    context.RotateCTM((nfloat)Math.PI);
                    context.TranslateCTM(-imageSize.Width / 2, -imageSize.Height / 2);
                    break;

                default:
                    break;
                }

                context.InterpolationQuality = CGInterpolationQuality.High;
                context.SetBlendMode(CGBlendMode.Copy);
                context.DrawImage(new CGRect(0, 0, imageRef.Width, imageRef.Height), imageRef);

                CGImage contextImage = context.ToImage();

                context.Dispose();

                if (contextImage != null)
                {
                    _originalImage = UIImage.FromImage(contextImage, originalImage.CurrentScale, UIImageOrientation.Up);

                    contextImage.Dispose();
                }

                imageRef.Dispose();

                BeginInvokeOnMainThread(() =>
                {
                    CGSize convertedImageSize = new CGSize(_originalImage.Size.Width / _cropSizeRatio,
                                                           _originalImage.Size.Height / _cropSizeRatio);

                    ImageView.Alpha = 0;
                    ImageView.Image = _originalImage;

                    CGSize sampleImageSize = new CGSize(Math.Max(convertedImageSize.Width, ScrollView.Frame.Size.Width),
                                                        Math.Max(convertedImageSize.Height, ScrollView.Frame.Size.Height));

                    ScrollView.MinimumZoomScale = 1;
                    ScrollView.MaximumZoomScale = 1;
                    ScrollView.ZoomScale        = 1;
                    ImageView.Frame             = new CGRect(0, 0, convertedImageSize.Width, convertedImageSize.Height);

                    nfloat zoomScale = 1;

                    if (convertedImageSize.Width < convertedImageSize.Height)
                    {
                        zoomScale = (ScrollView.Frame.Size.Width / convertedImageSize.Width);
                    }
                    else
                    {
                        zoomScale = (ScrollView.Frame.Size.Height / convertedImageSize.Height);
                    }

                    ScrollView.ContentSize = sampleImageSize;

                    if (zoomScale < 1)
                    {
                        ScrollView.MinimumZoomScale = zoomScale;
                        ScrollView.MaximumZoomScale = 1;
                        ScrollView.ZoomScale        = zoomScale;
                    }
                    else
                    {
                        ScrollView.MinimumZoomScale = zoomScale;
                        ScrollView.MaximumZoomScale = zoomScale;
                        ScrollView.ZoomScale        = zoomScale;
                    }

                    ScrollView.ContentInset  = UIEdgeInsets.Zero;
                    ScrollView.ContentOffset = new CGPoint((ImageView.Frame.Size.Width - ScrollView.Frame.Size.Width) / 2, (ImageView.Frame.Size.Height - ScrollView.Frame.Size.Height) / 2);
                    if (cropFrame.Size.Width > 0 && cropFrame.Size.Height > 0)
                    {
                        nfloat scale        = UIScreen.MainScreen.Scale;
                        nfloat newZoomScale = (_targetSize.Width * scale) / cropFrame.Size.Width;

                        ScrollView.ZoomScale = newZoomScale;

                        nfloat heightAdjustment = (_targetSize.Height / _cropSizeRatio) - ScrollView.ContentSize.Height;
                        nfloat offsetY          = cropFrame.Y + (heightAdjustment * _cropSizeRatio * scale);

                        ScrollView.ContentOffset = new CGPoint(cropFrame.X / scale / _cropSizeRatio,
                                                               (offsetY / scale / _cropSizeRatio) - heightAdjustment);
                    }

                    ScrollView.SetNeedsLayout();

                    UIView.Animate(0.3,
                                   () =>
                    {
                        LoadIndicator.Alpha = 0;
                        ImageView.Alpha     = 1;
                    }, () =>
                    {
                        LoadIndicator.StopAnimating();
                    });
                });
            });
        }