views:

1686

answers:

5

My application consists of a UIImageView inside a UIScrollView, and I display a big image inside of it. The scroll view allows the user to pinch to zoom in/out in the image, and that all seems to work just fine.

However, when my application is terminated and then re-launched, the UIScrollView displays the image again in the original zoom level (which is currently set to display the whole image, by scaling it in a "aspect fit" mode).

I would really like to be able to re-launch my app and have the UIScrollView reopen with the same parameters as it was set when the app terminated. So if my image is currently zoomed in to the max, and scrolled all the way to the bottom left of it, that should be the view when I open the app again.

How can I do this?

+2  A: 

Someone asked this a while ago, but I don't think you'll like the answer. Hopefully you'll get a better one.

Steven Fisher
+3  A: 

Check the .transform property of the view. If you are zoomed in, this should be modified. Save and restore it on the next launch.

Kailoa Kadano
+2  A: 

I have found a way to programmatically zoom UIScrollView. This may help you set the desired zoom level upon startup.

The sample code, together with ZoomScrollView class that encapsulates the required zooming magic and a detailed discussion of how (and why) UIScrollView zooming works is available at github.com/andreyvit/ScrollingMadness/.

Andrey Tarantsov
A: 

Hi Andrey, Could you please brief, how to set the desired zoom level?

Senthil
A: 

I do it like this: When you quit save the zoomlevel (myScrollview.zoomScale) and the contentview frame origin.

When you reopen you can set the zoomscale. You also have to set the content size to the new zoomlevel, because otherwise the boundaries are not correct.

    [myScrollview setZoomScale:savedZoomScale];
    CGSize newsize = CGSizeMake(origsize.width * savedZoomScale, 
                                 origsize.height * savedZoomScale);
    myScrollview.contentSize = newsize;

To set the offset:

[myScrollView setContentOffset:savedFrameOrigin animated:YES];
RickyA