我有一個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