我有一个 UIImageView
在 ScrollView
内.该应用程序允许用户在 ScrollView 中平移/缩放图像,然后应用程序保存 ContentOffset
和 ZoomScale
ScrollView 的数据。
当我重新加载 ViewController 时,我执行以下操作:
myImageView.image = UIImage.FromFile("image.jpg");
myScrollView.SetZoomScale(zoomScale, true);
myScrollView.SetContentOffset(contentOffset, true);
但是加载的图像不会出现在 ScrollView 中的相同位置。
最佳答案
为了保存缩放图像的状态,您需要保存缩放比例和偏移量。如果要使用这些变量加载相同的图像,则必须在更新 GUI 的方法中有一定的顺序(初始化控件、更改框架等...)
顺序应该是:
- 初始化您的控件:
UIImageView imageView = new UIImageView (this.view.bounds);
imageView.Image = image;
imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
imageView.ClipToBounds = true;
UIScrollView scrollView = new UIScrollView (this.View.Bounds);
scrollView.ContentSize = new SizeF (imageView.Frame.Width, imageView.Frame.Height);
scrollView.MaximumZoomScale = 4f;
scrollView.MinimumZoomScale = 1f;
scrollView.ViewForZoomingInScrollView += (UIScrollView sv) =>
{
return imageView;
};
- 将控件添加到正确的 View
this.View.AddSubview (scrollView);
scrollView.AddSubview (imageView);
- 完成后,您可以调整偏移量
scrollView.SetContentOffset (new PointF (50, 50), false);
- 你的缩放比例
scrollView.ZoomToRect (new RectangleF (50, 50, 100, 100), false);
希望这可以帮助!祝你好运!
关于c# - 图像不保留缩放比例和偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22277744/