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

标签 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

最佳答案

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

let image = startingImageView.image
let w = image.size.width
let h = image.size.height
print("image width: ", w)
print("image height: ", h)
var height = 0
if (w > h) { //landscape
    height = (self.startingFrame!.height / self.startingFrame!.width) * keyWindow.frame.width
} else { //portrait
    height = (self.startingFrame!.height / self.startingFrame!.width) * keyWindow.frame.height
}

关于ios - 根据不同的图像类型获取正确的图像高度的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45539946/

相关文章:

json - 针对 AnyObject JSON 错误的 Swift 2 到 3 迁移

ios - 多个 Segue 到同一目的地

ios - 查找 Image(systemName :) in SwiftUI) 的所有可用图像

ios - NSXMLParser 的错误处理

iphone - UIWebview 集成在 Cocos2d 游戏中?

ios - 属性 setter responseToSelector

swift - 如何在太多关系中请求多个值

ios - TableView 自动重新排序

objective-c - 在 Objective-C 中转换快速重复数组

ios - swift 3 iOS:将UInt8数组转换为int8