根据不同的图像类型获取正确的图像高度问题

时间:2017-08-07 05:30:52

标签: ios swift swift3 uiimageview

在我的应用程序中,我一直致力于点击图像并放大图像并显示完整尺寸的图像。

理想情况下,这就是我想要的工作方式: (我没有让我在这里添加视频,所以这里是指向它的链接) https://media.giphy.com/media/F7wCO7miVMG6k/source.mp4

但是,如果身高不高,我只能以正确的尺寸显示图像。 Short height Short height full screen

如果我有方形图像或图像太大,图像会显示如下: square image square image full

这是处理图像显示方式的代码:

var startingFrame: CGRect?
var blackBackgroundView: UIView?
var startingImageView: UIImageView?

func performZoomInForStartingImageView(startingImageView: UIImageView){
    print("starting image view: ", startingImageView.bounds)
    //startingImageView.sizeToFit()

    self.startingImageView = startingImageView
    self.startingImageView?.isHidden = true

    startingFrame = startingImageView.superview?.convert(startingImageView.frame, to: nil)
    print("starting frame: ", startingFrame!)

    let zoomingImageView = UIImageView(frame: startingFrame!)
    //zoomingImageView.backgroundColor = UIColor.red
    zoomingImageView.image = startingImageView.image
    print("zoom image view: ", zoomingImageView.bounds )
    zoomingImageView.isUserInteractionEnabled = true
    //zoomingImageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
    //zoomingImageView.contentMode = UIViewContentMode.scaleAspectFit
    zoomingImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleZoomOut)))

    if let keyWindow = UIApplication.shared.keyWindow {

        print("key window frame: ", keyWindow.frame)

        blackBackgroundView = UIView(frame: keyWindow.frame)
        blackBackgroundView?.backgroundColor = UIColor.black
        blackBackgroundView?.alpha = 0
        keyWindow.addSubview(blackBackgroundView!)
        keyWindow.addSubview(zoomingImageView)


        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

            self.blackBackgroundView?.alpha = 1

            print("keywindow frame width: ", keyWindow.frame.width)
            print("keywindow frame height: ", keyWindow.frame.height)
            print("\nstarting frame height: ", self.startingFrame!.height)
            print("starting frame width: ", self.startingFrame!.width)

            let height = (self.startingFrame!.height / self.startingFrame!.width) * keyWindow.frame.width
            print("height in animate:", height)
            //let height = self.startingFrame!.height * 2

            zoomingImageView.frame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: height)
            print("zooming image view frame: ", zoomingImageView.frame)

            zoomingImageView.center = keyWindow.center

        }, completion: nil)

    }
}

注意:我添加了一堆打印语句,以确切了解框架的内容

我还注意到我的问题发生在以下行:

let height = (self.startingFrame!.height / self.startingFrame!.width) * keyWindow.frame.width

通过使用keyWindow.frame.width,它可以正确显示身高较短的图像,但其他照片的高度不正确。

如果我将该行更改为:

let height = (self.startingFrame!.height / self.startingFrame!.width) * keyWindow.frame.height

它正确显示方形图像,但不显示高度较短的方形图像。

如何检测图像的正确高度并根据图像的高度正确显示?

修改 方形图像日志 Square image log

宽图像日志 Wide image log

编辑2: 新记录

方形图像日志 Square image

宽图像日志 Wide image

1 个答案:

答案 0 :(得分:1)

您可以尝试添加{{1}}条件来检查图像是横向还是纵向并相应地设置高度,

{{1}}