gpt4 book ai didi

ios - 异步加载内容时如何设置UIScrollView contensize

转载 作者:技术小花猫 更新时间:2023-10-29 10:35:09 28 4
gpt4 key购买 nike

我的 View 层次结构是这样的

PhotoDetailViewController.swift

View
UIScrollView
UIImageView

我使用 Storyboard设置它,并向 UIScrollView 添加四个约束(top=0、bottom=0、leading=0、tailing=0)、四个约束(top=0、bottom=0、leading=0、tailing =0) 到 UIImageView,但是有两个错误说

"ScrollView has ambiguous scrollable content width"
"ScrollView has ambiguous scrollable content height"

我知道这是因为我没有设置 UIScrollView contentSize,但我想做的是从 PHAsset 异步加载照片,所以我只能在运行时获取照片大小。所以问题是:

1:图片尺寸只能在运行时获取,如何解决"ambiguous scrollable content"错误?

2:应该在View的哪个生命周期方法中调用PHImageManager.requestImageForAsset?因为我认为我应该以编程方式设置 UIScrollView contentSize,但是什么时候?

使用 PhotoDetailViewController.swift 更新

import UIKit
import Photos

class PhotoDetailViewController : UIViewController {

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
@IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
@IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
@IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!

var devicePhotosAsset : PHFetchResult!
var index = 0
var photo : UIImage!
var imgManager:PHImageManager!

@IBOutlet weak var imageView : UIImageView!

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}

override func awakeFromNib() {
self.imgManager = PHImageManager()
}

override func viewDidLoad() {
super.viewDidLoad()
self.displayPhoto()
}

override func viewWillLayoutSubviews() {
super.viewDidLayoutSubviews()
updateMinZoomScaleForSize()
updateConstraintsForSize()
}

func displayPhoto () {
_ = self.imgManager.requestImageForAsset(self.devicePhotosAsset[self.index] as! PHAsset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFit, options: nil, resultHandler: {(result, info) -> Void in
NSOperationQueue.mainQueue().addOperationWithBlock(){
self.imageView.image = result
}

})
}

private func targetSize() -> CGSize {
let scale = UIScreen.mainScreen().scale
let targetSize = CGSizeMake(CGRectGetWidth(self.imageView.bounds)*scale, CGRectGetHeight(self.imageView.bounds)*scale)

return targetSize
}

private func updateMinZoomScaleForSize() {
let size = scrollView.bounds.size
let widthScale = size.width / imageView.bounds.width
let heightScale = size.height / imageView.bounds.height
let minScale = min(widthScale, heightScale)

scrollView.minimumZoomScale = minScale

scrollView.zoomScale = minScale
}

func recenterImage(){
let scrollViewSize = scrollView.bounds.size
let imageSize = imageView.frame.size

let horizontalSpace = imageSize.width < scrollViewSize.width ? (scrollViewSize.width - imageSize.width)/2 : 0
let verticalSpace = imageSize.height < scrollViewSize.height ? (scrollViewSize.height - imageSize.height)/2 : 0
scrollView.contentInset = UIEdgeInsets(top: verticalSpace, left: horizontalSpace, bottom: verticalSpace, right: horizontalSpace)
}

private func updateConstraintsForSize() {
let size = scrollView.bounds.size
let yOffset = max(0, (size.height - imageView.frame.height) / 2)
imageViewTopConstraint.constant = yOffset
imageViewBottomConstraint.constant = yOffset

let xOffset = max(0, (size.width - imageView.frame.width) / 2)
imageViewLeadingConstraint.constant = xOffset
imageViewTrailingConstraint.constant = xOffset

view.layoutIfNeeded()
}
}

extension PhotoDetailViewController: UIScrollViewDelegate {
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}

func scrollViewDidZoom(scrollView: UIScrollView) {
updateConstraintsForSize()
}
}

最佳答案

您现有的约束足以设置内容大小,只是它基于 ImageView 固有内容大小,并且在 ImageView 有图像之前并不存在。

您可以使用默认值向 ImageView 添加宽度和高度约束,并在图像设置到 View 时停用这些约束。或者您可以使用占位符图像并避免这些额外的限制,因为 ImageView 始终具有固有的内容大小。

关于ios - 异步加载内容时如何设置UIScrollView contensize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38735369/

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