9

I have an image like:

Want To Have

When I load the image to uiimageview and then adding as a subview to uiscrollview, at start the image is showing like:

Dont Want To Have

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];
aeciftci
  • 679
  • 2
  • 6
  • 15

5 Answers5

16

Probably you are looking for this,

UIImageView *tempImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Watson.jpg"]] autorelease];
tempImageView.frame = scrollView.bounds;
self.imageView = tempImageView;

scrollView.backgroundColor = [UIColor blackColor];
scrollView.minimumZoomScale = 1.0  ;
scrollView.maximumZoomScale = imageView.image.size.width / scrollView.frame.size.width;
scrollView.zoomScale = 1.0;
scrollView.delegate = self;

[scrollView addSubview:imageView];
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • Thank you so much it worked perfectly. My last question is, in this situation the image is not moveable, cant move it until zoomed. Is there a way to move it out of bounds of scrollview at start ? – aeciftci May 28 '11 at 17:27
  • Out of bounds? Do you mean that user has to scroll to image (initially not in scope) before zooming? – Deepak Danduprolu May 28 '11 at 17:29
  • Yea, for example the user will be able to move the image to left right up and down without zooming – aeciftci May 28 '11 at 17:32
  • 1
    Ok. If you want to be able to pan the image around, you should increase the `zoomScale` to a slightly higher value. If you want to move the image view within the bounds of the scroll view, then you should increase the content size and handle touches appropriately. – Deepak Danduprolu May 28 '11 at 17:38
  • Is it possible to draw an rectangle over there? – Gajendra K Chauhan Jun 06 '12 at 19:08
  • they key is in the tempImageView.frame = scrollView.bounds , this is where it all happens ! can't thank you enough this line of code literally saved my life ! – Elias Rahme Oct 22 '14 at 11:30
2

Have a look at the contentMode-Property of UIView (UIImageView also supports this).

imageView.contentMode = UIViewContentModeScaleAspectFit;

This should work...

Fabio Poloni
  • 8,219
  • 5
  • 44
  • 74
  • it didnt work it is the same I have the code below maybe i m missing something: imageView.contentMode = UIViewContentModeScaleAspectFit; [imageView sizeToFit]; 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 addSubview:imageView]; – aeciftci May 28 '11 at 13:09
  • 1
    Just delete some code to find out: Just create the image, add it so the view set the contentSize of the scrollView and add the View to the scrollView... – Fabio Poloni May 29 '11 at 10:14
1

I think the problem is in

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tree.jpg"]];

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

UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
tempImageView.image = [UIImage imageNamed:@"Tree.jpg"];
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
0

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:

- (void)layoutSubviews
{
[super layoutSubviews];

// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = _imageView.frame;

// center horizontally
if (frameToCenter.size.width < boundsSize.width)
    frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
    frameToCenter.origin.x = 0;

// center vertically
if (frameToCenter.size.height < boundsSize.height)
    frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
    frameToCenter.origin.y = 0;

_imageView.frame = frameToCenter;

// If image is bigger than frame, we zoom it on first load (zoom value is smaller than 1.0)
if (_firstLayout) {
    _firstLayout = NO;
    self.zoomScale = (self.frame.size.width < _imageView.image.size.width) ? (self.frame.size.width / _imageView.image.size.width) : 1.0;
}
}

This is how I init my subclass:

- (id)initWithFrame:(CGRect)frame image:(UIImage*)image
{
self = [super initWithFrame:frame];
if (self) {

    _firstLayout = YES;

    _imageView = [[UIImageView alloc] initWithImage:image];
    [self addSubview:_imageView];

    self.backgroundColor = [UIColor blackColor];
    self.minimumZoomScale = 0.1;
    self.maximumZoomScale = 10;


    self.delegate = self;
}
return self;
}
Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70
0

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.

Community
  • 1
  • 1
nembleton
  • 2,392
  • 1
  • 18
  • 20