Brodie
Brodie

Reputation: 3506

iphone uiscrollview and uiimageview - setting initial zoom

I use the following code to load an image into an scroll view. The image always loads at 100% zoom. Is there a way to set it to load to another zoom level, say .37?

I have tried scrollView.zoomScale = .37 but it didnt seem to work

UIImageView *tempImage = [[UIImageView alloc]initWithImage:[UIImage imageWithData:data]];
        self.imageView = tempImage;

        scrollView.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
        scrollView.maximumZoomScale = 1;
        scrollView.minimumZoomScale = .37;
        scrollView.clipsToBounds = YES;
        scrollView.delegate = self;

        [scrollView addSubview:imageView];

Upvotes: 7

Views: 20754

Answers (5)

Sankalp S Raul
Sankalp S Raul

Reputation: 69

I was facing same issue..
i used the built in method : scrollViewForImage

This will set the initial zoom to level 5

[self.scrollViewForImage setZoomScale:5 animated:YES];

code for minimum zoom level

self.scrollViewForImage.minimumZoomScale=1;

code for Maximum Zoom Level

self.scrollViewForImage.maximumZoomScale=12.0;

Hope this will help

Upvotes: 1

Elly
Elly

Reputation: 1

You must set the initial zoom when the viewwillappear and not when the viewdidload for it too work.

Upvotes: 0

Brodie
Brodie

Reputation: 3506

I figured it out... I was using scrollView.zoomScale = 0.37; before I loaded the image changed code and it works great.

UIImageView *tempImage = [[UIImageView alloc]initWithImage:[UIImage imageWithData:data]];
self.imageView = tempImage;

scrollView.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
scrollView.maximumZoomScale = 1;
scrollView.minimumZoomScale = .37;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
scrollView.zoomScale = .37;

Upvotes: 13

drawnonward
drawnonward

Reputation: 53669

Zooming only works when you implement the viewForZoomingInScrollView: delegate callback.

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)inScroll {
  return imageView;
}

Upvotes: 18

kennytm
kennytm

Reputation: 523334

There is a zoomScale property that you can set.

Upvotes: 3

Related Questions