将缩放图像中心放在Xamarin.iOS的UIScrollView中

时间:2017-12-05 05:43:02

标签: xamarin xamarin.ios uiscrollview uiimageview zoom

我想让UIImageView居中并执行Zoom on That。根据下面的代码,缩放工作正常。但我的UIImageView从NavigationBar下面开始。但是我想要UIImageView中心并放大中心并放大UIScrollView的contentSize。

代码:

scrollView = new UIScrollView();
scrollView.Frame = new CGRect(0, 0, View.Frame.Width, View.Frame.Height - 20.0f);
View.AddSubview(scrollView);

dicom_image = new UIImageView(new CGRect(0, 0, View.Frame.Size.Width,300));
dicom_image.Image = dicomItem.uiimage;

dicom_image.UserInteractionEnabled = true;

if (dicomItem.uiimage != null)
{
    dicom_image.Image = ExtensionMethods.MaxResizeImage(dicomItem.uiimage, (float)UIScreen.MainScreen.Bounds.Width, (float)UIScreen.MainScreen.Bounds.Width);
    dicom_image.ClipsToBounds = true;
    dicom_image.TranslatesAutoresizingMaskIntoConstraints = false;
    dicom_image.SizeToFit();
};
dicom_image.Center = scrollView.Center;


scrollView.ContentSize = scrollView.Frame.Size;
scrollView.AddSubview(dicom_image);

scrollView.MaximumZoomScale = 3f;
scrollView.MinimumZoomScale = 1f;
scrollView.ViewForZoomingInScrollView += (UIScrollView sv) => { return dicom_image; };

从上面的代码缩放工作正常,但我希望UIImageView成为中心。

修改:

此代码适用于缩放,但图像从头开始而不是从中心开始。

scrollView = new UIScrollView();
                            scrollView.Frame = new CGRect(0, 0, View.Frame.Width, View.Frame.Height - 20.0f);
                            View.AddSubview(scrollView);

                            dicom_image = new UIImageView(new CGRect(0, 0, View.Frame.Size.Width,300));
                            dicom_image.Image = dicomItem.uiimage;
                                        dicom_image.ContentMode = UIViewContentMode.Center;

                            dicom_image.UserInteractionEnabled = true;

                            if (dicomItem.uiimage != null)
                            {
                                dicom_image.Image = ExtensionMethods.MaxResizeImage(dicomItem.uiimage, (float)UIScreen.MainScreen.Bounds.Width, (float)UIScreen.MainScreen.Bounds.Width);
                                dicom_image.ClipsToBounds = true;
                                dicom_image.TranslatesAutoresizingMaskIntoConstraints = false;
                                dicom_image.SizeToFit();
                            };

                            dicom_image.Center = scrollView.Center;


                            scrollView.ContentSize = dicom_image.Frame.Size;
                            scrollView.AddSubview(dicom_image);

                scrollView.ShowsVerticalScrollIndicator = false;
                scrollView.ShowsHorizontalScrollIndicator = false;

                scrollView.MaximumZoomScale = 3f;
                scrollView.MinimumZoomScale = 1f;
                scrollView.ViewForZoomingInScrollView += (UIScrollView sv) => { return dicom_image; };

                UITapGestureRecognizer doubletap = new UITapGestureRecognizer(OnDoubleTap)
                {
                    NumberOfTapsRequired = 2 // double tap
                };
                scrollView.AddGestureRecognizer(doubletap);

任何帮助都要得到赞赏。

1 个答案:

答案 0 :(得分:1)

由于您没有使用约束,您可以设置UIImageView的边界。

这是一个条带化的示例,使用CIFilter来调整我用于Instagram查看器的图像的大小。

//someUIImageInstance is a CGImage-backed UIImage
var xScale = (float)(View.Frame.Size.Width / someUIImageInstance.CGImage.Width); 
var yScale = (float)(View.Frame.Size.Height / someUIImageInstance.CGImage.Height); 
var scale = Math.Max(xScale, yScale);
var lanczosScaleTransform = new CILanczosScaleTransform
{
    Image = someUIImageInstance.CGImage,
    Scale = scale
};
var yPosition = (scale * View.Frame.Size.Height) / 2;
imageView = new UIImageView(new CGRect(0, 0, View.Frame.Size.Width, View.Frame.Size.Height))
{
    Image = UIImage.FromImage(lanczosScaleTransform.OutputImage),
    Bounds = new CGRect(0, yPosition, View.Frame.Size.Width, View.Frame.Size.Height - yPosition),
    UserInteractionEnabled = true,
};
scrollView = new UIScrollView(new CGRect(0, 0, View.Frame.Width, View.Frame.Height))
{
    MinimumZoomScale = 1f,
    MaximumZoomScale = 3f,
    BackgroundColor = UIColor.LightGray
};
scrollView.ViewForZoomingInScrollView = (scrollView) => { return imageView; };

scrollView.AddSubview(imageView);
View.AddSubview(scrollView);

enter image description here

注意:如果在UIImageView居中并在图像缩放时控制视口中心方面使用约束会更容易...