计算UIScrollView的minimumZoomScale

时间:2010-10-06 05:47:18

标签: iphone objective-c xcode

我想要将图像加载到图像视图中,并将minimumZoomScale和zoomScale设置为像我所计算的尺度一样的aspectFill,如下所示:

// configure the map image scroll view
iImageSize = CGSizeMake(iImageView.bounds.size.width, iImageView.bounds.size.height);
iScrollView.minimumZoomScale = iScrollView.bounds.size.height / iImageSize.height;
iScrollView.maximumZoomScale = 2;
iScrollView.zoomScale = iScrollView.minimumZoomScale;
iScrollView.contentOffset = CGPointMake(0, 0);
iScrollView.clipsToBounds = YES;

iScrollView尺寸为450 x 320px,iImageSize为1600 x 1960px。

手动执行minimumZoomScale数学:450/1960 = 0.22959184。 系统确定为0.234693885(???)。

但要使窗口适合450px空间,两个数字都不起作用(!!!)。 我手动尝试,发现0.207是正确的数字(这将转换为2174 xp的图像高度或者UIScrollView高度为406px)。

有关信息:UIScrollview屏幕为450px,即480px减去状态栏高度(10),减去UITabBar高度(20)

关于这种不端行为的任何线索?

2 个答案:

答案 0 :(得分:0)

不确定你是否还有这个问题,但对我而言,规模恰恰相反。 minimumZoomScale = 1对我而言,我必须计算maximumZoomScale

以下是代码:

[self.imageView setImage:image];
// Makes the content size the same size as the imageView size.
// Since the image size and the scroll view size should be the same, the scroll view shouldn't scroll, only bounce.
self.scrollView.contentSize = self.imageView.frame.size;

// despite what tutorials say, the scale actually goes from one (image sized to fit screen) to max (image at actual resolution)
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat minScale = 1;
// max is calculated by finding the max ratio factor of the image size to the scroll view size (which will change based on the device)
CGFloat scaleWidth = image.size.width / scrollViewFrame.size.width;
CGFloat scaleHeight = image.size.height / scrollViewFrame.size.height;
self.scrollView.maximumZoomScale = MAX(scaleWidth, scaleHeight);
self.scrollView.minimumZoomScale = minScale;
// ensure we are zoomed out fully
self.scrollView.zoomScale = minScale;

答案 1 :(得分:-1)

对于简单的放大/缩小,我通常使用下面的代码。 minimumZoomScale = 1将初始缩放设置为当前图像大小。我给出了maximumZoomScale = 10,它可以根据图像大小和容器框架大小的实际比例来计算。

[scrollView addSubview: iImageView];
    scrollView.contentSize = iImageView.frame.size;
    scrollView.minimumZoomScale = 1.0;
    scrollView.maximumZoomScale = 10;
    [iImageView setFrame:CGRectMake(0, 0, 450, 320)];
[scrollView scrollRectToVisible:iImageView.frame animated:YES];