gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:14:37 25 4
gpt4 key购买 nike

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

理想情况下,这是我想要开始工作的:(没有让我在这里添加视频,所以这是它的链接) 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/

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