早上好 !
我有一个关于图片下载和约束的设计问题。
我目前使用 Haneke 在后台下载我的图像。它就像一个魅力,但我可以拥有很多不同的图像尺寸格式(我一次显示一个图像)。
所以我的问题是我根据图像的比例调整图像以适应不同的尺寸。它工作得很好:
if let url = NSURL(string: coverPhoto.url) {
self.coverImageView?.hnk_setImageFromURL(url,format: nil, failure: nil,success: { image in
// Screen Width
let screen_width = UIScreen.mainScreen().bounds.width
// Ratio Width / Height
let ratio = image.size.height / image.size.width
// Calculated Height for the picture
let newHeight = screen_width * ratio
self.__coverImageViewHeightLayoutConstraint?.constant = newHeight
self.coverImageView?.image = image
})
}
我使用原型单元格来显示我的内容。因此,当该代码完成后,我重新加载封面图像单元格以应用/更新新布局。到目前为止,一切都很好 !!
但我的问题是,当图像看起来根本不好看并且正在调整大小时,应用新约束需要一点时间,这会导致 UI 看起来很奇怪/不好看,因为你可以看到它正在发生:=/
我使用的另一种方法是简单地同步加载图像:
if let dataUrl = NSData(contentsOfURL: url)
{
let image = UIImage(data: dataUrl)
// Screen Width
let screen_width = UIScreen.mainScreen().bounds.width
// Ratio Width / Height
let ratio = image!.size.height / image!.size.width
// Calculated Height for the picture
let newHeight = screen_width * ratio
self.__coverImageViewHeightLayoutConstraint?.constant = newHeight
self.coverImageView!.image = image!
}
但显然,出于性能原因/滚动问题,我想避免这种情况(向上滚动将再次下载图像,因为它没有被缓存)。
最好的方法是什么?
谢谢 !