问题描述
我有一个UIImageView容器,设置在这里:
let moviePosterContainer = UIImageView()
customView.addSubview(moviePosterContainer)
moviePosterContainer.translatesAutoresizingMaskIntoConstraints = false
moviePosterContainer.isUserInteractionEnabled = true
moviePosterContainer.layer.cornerRadius = 10
moviePosterContainer.clipsToBounds = true
moviePosterContainer.centerXAnchor.constraint(equalTo: customView.centerXAnchor).isActive = true
moviePosterContainer.centerYAnchor.constraint(equalTo: customView.centerYAnchor).isActive = true
我如何添加UImage:
moviePosterContainer.sd_setImage(with: URL(string: moviePosterPath),placeholderImage: UIImage(),options: [],completed: { (image,error,cacheType,url) in
// here image is the UIImage
print("image size height width")
print(image.size.height)
print(image.size.width)
print("image sizes")
//let ratio = (image?.size.width)! / (image?.size.height)!
if let image = image {
let ratio = image.size.width / image.size.height
print("image ratio w")
print(ratio)
print("image ratio h")
print(image.size.height / image.size.width)
if customView.frame.width > customView.frame.height {
print("is first")
let newHeight = customView.frame.width / ratio
print("new height")
print(newHeight)
moviePosterContainer.frame.size = CGSize(width: customView.frame.width,height: newHeight)
print("container frame size first")
print(moviePosterContainer.frame.size)
print("container ratio")
print(newHeight / customView.frame.height)
}
else{
print("is second")
let newWidth = customView.frame.height * ratio
print("new width")
print(newWidth)
moviePosterContainer.frame.size = CGSize(width: newWidth,height: self.kolodaView1.frame.height)
print("container frame size second")
print(moviePosterContainer.frame.size)
print("container ratio w")
print(newWidth / customView.frame.height)
print("container ratio h")
print(customView.frame.height / newWidth)
}
}
moviePosterContainer.image = image
moviePosterContainer.contentMode = .scaleAspectFit
})
控制台输出:
image size height width
750.0
500.0
image ratio w
0.6666666666666666
image ratio h
1.5
is second
new width
288.0
container frame size second
(288.0,440.0)
container ratio w
0.6666666666666666
container ratio h
1.5
您可以看到,我的UImageView与UImage的比率相同。
但是我在UImageView中的UIImage仍然无法正确显示。看起来UImage仍然比我的容器大。 ScaleToFit无效。
为什么UIImage无法正确显示?是因为原始图像尺寸大吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)