UIImage is not suitable for UIScrollView at startup

I have an image like:

Want to have

When I load an image in uiimageview and then add it as a subview in uiscrollview, when I start the image looks like this:

Dont want to have

The problem is that I want the whole image to fit the screen at startup, but it has already increased. Is there any way to handle this, please help ...

I have 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]; 
+8
iphone uiimage uiscrollview uiimageview
source share
5 answers

You are probably 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]; 
+15
source share

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

 imageView.contentMode = UIViewContentModeScaleAspectFit; 

This should work ...

+2
source share

I think the problem is

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

Apple docs say, "This method adjusts the receiver frame to fit the size of the specified image, and also disables user interactions to represent the default image." Use instead

 UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)]; tempImageView.image = [UIImage imageNamed:@"Tree.jpg"]; 
+1
source share

Note that there is also [scrollView zoomToRect: animated:] described here

You can set it to the outer borders of your image so that it automatically matches your image in your view. But do not forget to set the maximum ZoomScale maximum size according to the Deepak solution above .

0
source share

I used the Deepak and Apples solutions and subclassed the UIScrollView with the UIImageView inside it. However, I only zoom in on the first layout of my scrollview subclass, and only if the frame size is smaller than the 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 run 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; } 
0
source share

All Articles