IOS滚动查看默认比例图像大小

时间:2014-04-13 17:27:11

标签: ios objective-c uiscrollview uiimageview

我有scrollview包含图像,可以缩放图像。我希望默认缩放大小包含我附加到它的任何图像的整个图像。我不知道该怎么做。

以下是我目前的代码:

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

2 个答案:

答案 0 :(得分:0)

所以听起来你希望图像“适合”滚动视图。我希望这是正确的。如果是这样,请尝试以下代码。

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

该代码将使您的图像适合屏幕。所以当你开始时它不会被缩放。

答案 1 :(得分:0)

如果我理解正确,您需要一种可用于显示任意图像的方法。你需要这样的东西:

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

}

你可以像这样调用这个方法:

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