简体   繁体   English

iOS滚动视图默认缩放图像大小

[英]IOS Scroll View Default Scale Image Size

I have a scrollview that contains an image and it is able to zoom the image. 我有一个包含图像的scrollview ,它能够缩放图像。 I want the default zoom size to contain the whole image for any image that I attach to it. 我希望默认缩放大小包含我附加到其上的任何图像的整个图像。 I don't know how to do this. 我不知道该怎么做。

Here is the code I currently have so far: 这是我目前拥有的代码:

UIImage *image = [UIImage imageNamed:@"cat.jpg"];

self.imageView = [UIImageView new];
self.imageView.image = image;
[self.imageView sizeToFit];

self.scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.scroll.contentSize = image.size;
self.scroll.delegate = self;
self.scroll.minimumZoomScale = 0.2;
self.scroll.maximumZoomScale = 1.0;
self.scroll.zoomScale = 0.2;

[self.scroll addSubview:self.imageView];

[self.view addSubview:self.scroll];

So it sounds like you want the images to 'fit' the scroll view. 因此,听起来您想让图像“适合”滚动视图。 I hope thats correct. 我希望那是正确的。 If so, try the code below. 如果是这样,请尝试下面的代码。

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];

That code will fit your image to the screen. 该代码将使您的图像适合屏幕。 So it will not be zoomed when you start. 因此开始时不会缩放。

If I understand you correctly you need a method that you could use to display any image as you wish. 如果我对您的理解正确,则需要一种可以根据需要显示任何图像的方法。 You'd need something like this: 您需要这样的东西:

-(void)setImageOnScrollView:(UIImage*)image {

self.imageView.image = image;
[self.imageView sizeToFit];

self.scroll.contentSize = image.size;
self.scroll.delegate = self;
self.scroll.minimumZoomScale = 0.2;
self.scroll.maximumZoomScale = 1.0;
self.scroll.zoomScale = 0.2;

[self.scroll addSubview:self.imageView];
[self.view addSubview:self.scroll];

}

And you can call this method like so: 您可以这样调用此方法:

self.scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
UIImage *image = [UIImage imageNamed:@"cat.jpg"];
[self setImageOnScrollView:image];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM