UIScrollView zoomScale 不一致
UIScrollView zoomScale inconsistent
下面的代码 运行s 符合我第一次从相机胶卷中选择图像的预期。图像已缩小 属性 以适合 scrollView。但是当我 运行 再次重新选择图像并再次选择相同图像时,它会缩放到 1.0 比例。无法弄清楚我需要重置什么才能使它每次都能正常工作。有什么想法吗?
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
scrollView.contentSize = CGSize(width: 0, height: 0)
picView.image = nil
picView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
picView.removeFromSuperview()
self.dismissViewControllerAnimated(true, completion: nil)
scrollView.addSubview(picView)
scrollView.contentSize = image.size
let scaleWidth = scrollView.frame.size.width / scrollView.contentSize.width //image.size.width
let scaleHeight = scrollView.frame.size.height / scrollView.contentSize.height //image.size.height
let minScale = min(scaleWidth, scaleHeight)
scrollView.minimumZoomScale = minScale
scrollView.maximumZoomScale = 1.0
scrollView.zoomScale = minScale
}
我想您已经明白了这一点,但我将填补可能缺失的部分。缩放实际上是如何工作的?正如我所说 in my book:
The scroll view zooms by applying a scaling transform to the scalable view... Moreover, the scroll view is concerned to make scrolling continue to work correctly; ... therefore, the scroll view automatically scales its own contentSize to match the current zoomScale.
因此您需要在开始调整内容大小之前删除该转换。因此,在您换出图像视图之前,预先将 zoomScale
重置为 1 可能就足够了。
更好的是,为什么不把图像视图留在那里呢?您似乎在日常工作中做了很多不必要的工作;为什么不将其视为具有图像视图内容视图的滚动视图,其中换出的只是图像?
最后,为什么不使用自动布局呢?这样,图像的大小自动成为图像视图的大小,图像视图的大小自动成为滚动视图的内容大小。
下面的代码 运行s 符合我第一次从相机胶卷中选择图像的预期。图像已缩小 属性 以适合 scrollView。但是当我 运行 再次重新选择图像并再次选择相同图像时,它会缩放到 1.0 比例。无法弄清楚我需要重置什么才能使它每次都能正常工作。有什么想法吗?
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
scrollView.contentSize = CGSize(width: 0, height: 0)
picView.image = nil
picView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
picView.removeFromSuperview()
self.dismissViewControllerAnimated(true, completion: nil)
scrollView.addSubview(picView)
scrollView.contentSize = image.size
let scaleWidth = scrollView.frame.size.width / scrollView.contentSize.width //image.size.width
let scaleHeight = scrollView.frame.size.height / scrollView.contentSize.height //image.size.height
let minScale = min(scaleWidth, scaleHeight)
scrollView.minimumZoomScale = minScale
scrollView.maximumZoomScale = 1.0
scrollView.zoomScale = minScale
}
我想您已经明白了这一点,但我将填补可能缺失的部分。缩放实际上是如何工作的?正如我所说 in my book:
The scroll view zooms by applying a scaling transform to the scalable view... Moreover, the scroll view is concerned to make scrolling continue to work correctly; ... therefore, the scroll view automatically scales its own contentSize to match the current zoomScale.
因此您需要在开始调整内容大小之前删除该转换。因此,在您换出图像视图之前,预先将 zoomScale
重置为 1 可能就足够了。
更好的是,为什么不把图像视图留在那里呢?您似乎在日常工作中做了很多不必要的工作;为什么不将其视为具有图像视图内容视图的滚动视图,其中换出的只是图像?
最后,为什么不使用自动布局呢?这样,图像的大小自动成为图像视图的大小,图像视图的大小自动成为滚动视图的内容大小。