pnavk
pnavk

Reputation: 4630

Image not retaining zoom scale and offset

I have an UIImageView within a ScrollView. The app allows the user to pan/zoom the image within the scroll view and then the app saves the ContentOffset and ZoomScale data of the scroll view.

When I reload the ViewController, I do the following:

myImageView.image = UIImage.FromFile("image.jpg");
myScrollView.SetZoomScale(zoomScale, true);
myScrollView.SetContentOffset(contentOffset, true);

But the loaded image does not appear in the same position in the scroll view.

Upvotes: 1

Views: 814

Answers (1)

Magicbjørn
Magicbjørn

Reputation: 619

In order to save the state of your zoomed image, you need to save the zoomscale and the offset. If you want to load the same image with those variables, you have to have a certain order in the method where you update your GUI (initialise controls, change frames, etc...)

The order should be: - Initialise your controls:

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;
};

- Add the controls to the right views

this.View.AddSubview (scrollView);

scrollView.AddSubview (imageView);

- After that's done, you can adjust your offset

scrollView.SetContentOffset (new PointF (50, 50), false);

- And your zoom scale

scrollView.ZoomToRect (new RectangleF (50, 50, 100, 100), false);

Hope this helps! Good luck!

Upvotes: 3

Related Questions