2014-01-13 45 views
0

我设置了UIScrollView,其中包含UIImageViewUIImage。我加载到图像视图中的UIImage大小约为2300x1200,不能缩放,因为可以缩放。UIScrollView的布局不正确,UIScrollView在IOS的方向更改后保存UIImageView

UIScrollView包含一个UIImageView,它被设置为允许点击/双击来放大和缩小图像。我有followed a tutorial here to create this

该示例的源代码可以是downloaded here.

问题
我遇到的问题是改变方向。意见不再按预期排列并抵消。

一旦下载图像(在我的例子),我完成以下步骤:

[self.mainImageView setImage:image]; 
self.mainImageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size}; 

// Tell the scroll view the size of the contents 
self.mainScrollView.contentSize = image.size; 

[self setupScales]; 

然后我setupScales如下:

// Set up the minimum & maximum zoom scales 
CGRect scrollViewFrame = self.mainScrollView.frame; 
CGFloat scaleWidth = scrollViewFrame.size.width/self.mainScrollView.contentSize.width; 
CGFloat scaleHeight = scrollViewFrame.size.height/self.mainScrollView.contentSize.height; 
CGFloat minScale = MIN(scaleWidth, scaleHeight); 

self.mainScrollView.minimumZoomScale = minScale; 
self.mainScrollView.maximumZoomScale = 1.0f; 
self.mainScrollView.zoomScale = minScale; 

[self centerScrollViewContents]; 

然后我centerScrollViewContents如下:

CGSize boundsSize = self.mainScrollView.bounds.size; 
CGRect contentsFrame = self.mainImageView.frame; 

if (contentsFrame.size.width < boundsSize.width) { 
    contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0f; 
} else { 
    contentsFrame.origin.x = 0.0f; 
} 

if (contentsFrame.size.height < boundsSize.height) { 
    contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0f; 
} else { 
    contentsFrame.origin.y = 0.0f; 
} 

self.mainImageView.frame = contentsFrame; 

scrollView和imageView使用以下设置:

self.mainScrollView.translatesAutoresizingMaskIntoConstraints = NO; 
self.mainImageView.translatesAutoresizingMaskIntoConstraints = NO; 

scrollView对左侧/右侧/顶部/底部约束固定为0,并且imageView没有手动创建的约束。我试图在willRotateToInterfaceOrientation中运行self.mainScrollView needsUpdateConstraints,但这并没有什么区别。

当选择第一个选项“图像缩放”,然后更改设备的方向时,可以在上面的示例应用程序中复制相同的问题。我不确定什么是不正确的,它出现的框架设置不正确。我试图在不同的点运行setupScales,但这似乎并没有改变这个问题。

回答

0

看起来,scrollView的contentSize会在框架更改时重置。旋转完成后,我必须将其设置回图像大小。

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    // When the orientation is changed the contentSize is reset when the frame changes. Setting this back to the relevant image size 
    self.mainScrollView.contentSize = self.mainImageView.image.size; 
    // Reset the scales depending on the change of values 
    [self setupScales]; 
} 

我已经安装了任何人回购,在未来需要这个 - https://github.com/StuartMorris0/SPMZoomableUIImageView

相关问题