我有一个tableview的tableview单元格内的imageview。我想达到什么是应该根据它包含图像调整ImageView的dynamically.Currently我有这样的:故事板imageview在tableview单元格
0
A
回答
0
我不久前有这个完全相同的问题。
首先,您应该将图像内容模式设置为Aspect Fit,但这并不足以支持但是。
您每次需要加载新图像时都必须更改imageView的宽高比约束。基本上,你需要做的是:
- 获取图像的宽度和高度
- 计算宽高比
let aspectRatio = Float(image.height)/Float(image.width)
- 使用的是长宽比为您创造图像视图的新高宽比的约束。
这是我的代码的复制粘贴(与添加评论)我管理这个问题。希望它能帮助你。
// aspectRatioCnst is IBOutlet reference to aspect ratio constraint I've set on mainImageView in Storyboard
if let aspectRatioCnst = aspectRatioCnst {
aspectRatioCnst.isActive = false
}
let aspectRatio = Float(imagePost.height)/Float(imagePost.width)
aspectRatioCnst = NSLayoutConstraint(
item: self.mainImageView,
attribute: NSLayoutAttribute.height,
relatedBy: NSLayoutRelation.equal,
toItem: self.mainImageView,
attribute: NSLayoutAttribute.width,
multiplier: CGFloat(aspectRatio),
constant: 0)
if let aspectRatioCnst = aspectRatioCnst {
self.mainImageView.addConstraint(aspectRatioCnst)
aspectRatioCnst.isActive = true
self.mainImageView.layoutIfNeeded()
}
if let image = imagePost.loadedImage {
self.mainImageView.image = image
} else if let imageURL = URL(string: imagePost.fileURL) {
DispatchQueue.global(qos: .background).async {
// UIImage extension with downloadedFromURL method
UIImage.downloadedFromURL(url: imageURL, withCallback: { (image) -> (Void) in
DispatchQueue.main.async {
self.imageLoadingActivityIndicator.stopAnimating()
self.mainImageView.image = image
self.imagePost?.loadedImage = image
}
})
}
}
0
首先设置您的底部内容这样的高度,
然后设置你的形象约束这样的,
那么做到这一点在你的代码,
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let imageNew = UIImage(named: "test") //Set your image here
let oldWidth = imageNew!.size.width
let scaleFactor = tableView.frame.size.width/oldWidth
let newHeight = imageNew!.size.height * scaleFactor
let newWidth = oldWidth * scaleFactor
//Finally to get cell size just add the bottom part height for othercontents to ImageHeight here
let CellSize = CGSize(width: newWidth, height: (newHeight + 40))
return CellSize.height
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
cell.NewImage.image = UIImage(named: "test")
return cell
}
相关问题
- 1. Tableview零,但存在故事板
- 2. 无法在故事板的uicollection视图中移动单元格?
- 3. 在故事板中设计点击展开单元格
- 4. 图像不适合imageview在自定义tableview单元格
- 5. 表格单元不连接到iOS故事板的插座
- 6. 如何连接故事板中的原型单元格?
- 7. 如何调整故事板中原型单元格的面积
- 8. 在故事板
- 9. 在故事板
- 10. 在故事板
- 11. 在故事板
- 12. 在故事板
- 13. 在故事板
- 14. 故事板元素层
- 15. 风格的故事板
- 16. iOS 8.3故事板菜单
- 17. IOS处理事件单击按钮在tableView单元格
- 18. 从故事板中的表格单元格中显示多个segue
- 19. TableView单元格扩展不会发生在单个单元格
- 20. 如何在我的故事板中更改单元格文本标签
- 21. 在tableview后添加其他对象到iOS 5故事板
- 22. 我可以在故事板中设置分组tableView段号吗?
- 23. iPhone - 在单独的窗口中编辑故事板元素
- 24. 的TableView单元格颜色
- 25. 带空单元格的Tableview
- 26. 自定义tableview单元格
- 27. TableView自定义单元格
- 28. Cache tableView单元格数据
- 29. TableView显示空单元格
- 30. PickerView里面tableView单元格
如果什么形象是异步下载。那么代码将如何知道图像的高度和宽度? –
这是一个很好的观点。我以为我不必复制那部分,但自从你问了以后,我更新了我的答案。附:在指向图片URL的响应JSON中,我得到了图片的高度和宽度,所以我不必计算它。 – JPetric
基本上,我首先下载所有图像元数据,其中我有每个图像的高度和宽度以及我可以下载图像的URL。然后当我出列表格视图单元格时,我只是异步下载图像。希望我澄清一点。 – JPetric