是否有办法在从URL下载(或部分下载)图像之前检查图像尺寸(即高度和宽度)?我已经找到了获取图像大小的方法,但这无济于事。
基本上,我想在下载图像之前计算UITableView行的正确高度。这个是可能的吗?
发布于 2015-05-27 09:43:11
您可以下载部分图像数据,然后从中提取图像大小。您必须获取您正在使用的图像格式的数据结构,并在某种程度上对其进行解析。如果你能够进行低级编码,这是可能的,而且也不是很难。
发布于 2018-11-26 20:23:55
创建一个HeightConstraint的IBOutlet,例如这里的图像是16:9的比例,从服务器,这将自动调整所有屏幕的高度。ImageView ContentMode is AspectFit
override func viewDidLoad() {
cnstHeight.constant = (self.view.frame.width/16)*9
}
发布于 2017-07-18 21:06:37
您可以通过在Swift 3.0中访问其标题详细信息来执行此操作,下面的代码将帮助您,
if let imageSource = CGImageSourceCreateWithURL(url! as CFURL, nil) {
if let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary? {
let pixelWidth = imageProperties[kCGImagePropertyPixelWidth] as! Int
let pixelHeight = imageProperties[kCGImagePropertyPixelHeight] as! Int
print("the image width is: \(pixelWidth)")
print("the image height is: \(pixelHeight)")
}
}
https://stackoverflow.com/questions/30471362
复制相似问题