如何检测图像是关于imageView缩放的纵向和横向模式

时间:2017-05-24 16:22:02

标签: ios swift image uiscrollview uiimageview

我正在练习如何在Swift中使用imageView。

我最近尝试在UIScrollView中进行图像缩放,但我的问题是我不知道如何检查纵向和横向图像大小。 我的ImageView中的肖像图像看起来很奇怪。

我做错了什么以及如何在scrollView中设置不同的imageView大小?

当我在imageView中设置约束时,imageview将无法缩放。这是我的代码:

class imageViewController: UIViewController, UIScrollViewDelegate {

    var url:String = ""
    var id:String = ""

    var scrollView:UIScrollView = UIScrollView()
    var imageView:UIImageView = UIImageView()

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

    override func viewDidLoad() {
        super.viewDidLoad()

        self.automaticallyAdjustsScrollViewInsets = false
        imageView.contentMode = .scaleAspectFit
        imageView = UIImageView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height*0.2248, width: UIScreen.main.bounds.width, height:UIScreen.main.bounds.height*0.3748))

        imageFromUrl(imageView: imageView, url: url, id: id,isResize: false)

        scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
        scrollView.contentSize.width = UIScreen.main.bounds.width
        scrollView.contentSize.height = UIScreen.main.bounds.height*0.3748
        scrollView.minimumZoomScale = 1
        scrollView.maximumZoomScale = 5
        scrollView.delegate = self
        scrollView.addSubview(imageView)
        view.addSubview(scrollView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - UIScrollViewDelegate    
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
}

[portrait image like this]

0 个答案:

没有答案