gpt4 book ai didi

ios - UIScrollView zoomScale 不一致

转载 作者:搜寻专家 更新时间:2023-11-01 06:43:43 24 4
gpt4 key购买 nike

下面的代码在我第一次从相机胶卷中选择图像时按预期运行。图像被缩小以适应 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.

因此,在开始调整内容大小之前,您需要删除该转换。因此,在您换出 ImageView 之前,预先将 zoomScale 重置为 1 可能就足够了。

更好的是,为什么不把 ImageView 留在那里呢?您似乎在日常工作中做了很多不必要的工作;为什么不将其视为带有 ImageView 内容 View 的 ScrollView ,其中被换出的只是图像?

最后,为什么不使用自动布局呢?这样,图像的大小自动成为 ImageView 的大小, ImageView 的大小自动成为 ScrollView 的内容大小。

关于ios - UIScrollView zoomScale 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32798630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com