I have an image like:
When I load the image to uiimageview and then adding as a subview to uiscrollview, at start the image is showing like:
The problem is I want to see all the image fit to screen at start but it is showing already zoomed. Is there a way to handle this please help ...
I have the code like:
[self.view addSubview:scrollView];
UIImageView *tempImageView =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tree.jpg"]];
self.imageView = tempImageView;
[tempImageView release];
[scrollView setContentMode:UIViewContentModeScaleAspectFit];
[imageView sizeToFit];
scrollView.backgroundColor=[UIColor blackColor];
scrollView.contentSize = imageView.frame.size;
scrollView.minimumZoomScale = scrollView.frame.size.width / imageView.frame.size.width;
scrollView.maximumZoomScale = 4.0;
[scrollView setZoomScale:1.0];
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
Note that there is also [scrollView zoomToRect:animated:] described here
You can set it to the outer bounds of your image so that it will auto-fit your image in your view. But make sure to setup correctly the maximumZoomScale as per Deepak solution above.
I think the problem is in
Apple's docs say "This method adjusts the frame of the receiver to match the size of the specified image. It also disables user interactions for the image view by default."
instead use
Have a look at the contentMode-Property of UIView (UIImageView also supports this).
This should work...
I used Deepak's solution and Apples recommendations and subclassed UIScrollView with UIImageView inside it. However, I zoom the image only on first layout of my scrollview subclass and only if frame size is smaller than image size:
This is how I init my subclass:
Probably you are looking for this,