[英]How to resize UIImageView in UITableViewCell?
I want to display image in cell, and because I need to download it first so I would like to display (for question simplicity) black view with the same width and height like image should be displayed. 我想在单元格中显示图像,并且因为我需要先下载它,所以我想显示(为简单起见)具有与图像相同的宽度和高度的黑色视图。
Because I want to stretch image to same width as cell width, I only need aspect ratio for setting height and this is provided in my code when cellForRowAt
is called. 因为我想将图像拉伸到与单元格宽度相同的宽度,所以我只需要设置纵横比即可设置高度,这在调用
cellForRowAt
时在我的代码中提供。
I decided to achieve "black view" before downloading image I only need one UIImageView with black background, and resizing it when cellForRowAt
is called. 我决定在下载图像之前实现“黑视图”,我只需要一个具有黑色背景的UIImageView,并在调用
cellForRowAt
时调整其大小。 But here is the problem, because using code from similiar questions is not working for me. 但这是问题所在,因为使用类似问题的代码对我不起作用。
I tried something like this in cellForRowAt
method: 我在
cellForRowAt
方法中尝试过这样的事情:
var frame cell.imageView.frame
frame.size.height = CGFloat(aspectRatio) * frame.size.width
cell.imageView.frame = frame
EDIT 编辑
As a result I want something like facebook, where we have photo with the same width as cell and height accordingly to aspect ratio. 结果,我想要像facebook这样的东西,其中我们的照片具有与单元格相同的宽度和与宽高比相对应的高度。 For simplicity cell can only have UIImageView.
为简单起见,单元格只能具有UIImageView。
Since you're using auto layout, in the storyboard you should set two things: 由于您使用的是自动布局,因此在情节提要中应设置以下两项:
UIImageView
to be the full width of the cell UIImageView
的宽度设置为单元格的完整宽度 UIImageView
to 1:1 - this says the height is always the same as the width. UIImageView
的宽高比设置为1:1-这表示高度始终与宽度相同。 If you're using the UITableView
's Automatic Dimensions for cell heights, this should be all you need; 如果您要使用
UITableView
“ 自动尺寸”作为单元格高度,那么这应该就足够了; no code anywhere, everything else is as normal. 任何地方都没有代码,其他一切都正常。
If you're not using the automatic cell height feature, then either set the cell height once with self.tableView.rowHeight = self.tableView.bounds.size.width
if they're all the same, or, if the cells can be different heights: 如果您没有使用自动像元高度功能,则如果它们都相同,或者使用相同的像元,或者使用
self.tableView.rowHeight = self.tableView.bounds.size.width
设置一次像元高度。不同的高度:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (<indexPath IS IMAGE ROW>)
return tableView.bounds.size.width
return <default height>
}
Where I've written <indexPath IS IMAGE ROW>
, you should replace that with whatever condition you need to detect if this particular cell is an image type. 在我写过
<indexPath IS IMAGE ROW>
,应将其替换为需要检测该特定单元格是否为图像类型的任何条件。 Also, as Rikh pointed out, where I've written <default height>
, you should replace that with the height of the cells that are not the image type. 另外,正如Rikh所指出的那样,在我写
<default height>
,应将其替换为非图像类型的单元格的高度。 Of course, you may have a much more complex design for your table; 当然,您的桌子可能设计得更加复杂。 this just covers the case you were asking about.
这仅涵盖了您要询问的情况。
here my solution 这是我的解决方案
imageView.contentMode = UIViewContentModeCenter;
if (imageView.bounds.size.width > ((UIImage*)imagesArray[i]).size.width && imageView.bounds.size.height > ((UIImage*)imagesArray[i]).size.height) {
imageView.contentMode = UIViewContentMode.ScaleAspectFit;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.