tableview单元格中的故事板imageview

时间:2016-12-16 08:17:31

标签: ios swift uitableview uiimageview storyboard

我在tableview的tableview单元格中有一个imageview。我想要实现的是它应该根据动态包含的图像调整imageview的大小。目前我喜欢这样:

enter image description here

2 个答案:

答案 0 :(得分:0)

不久前我也有同样的问题。

首先,你应该将图像内容模式设置为Aspect Fit,但是是不够的。

每次要加载新图像时,都必须更改imageView的宽高比约束。基本上,您需要做的是:

  1. 获取图片的宽度和高度
  2. 计算宽高比let aspectRatio = Float(image.height) / Float(image.width)
  3. 使用该宽高比为图像视图创建新的宽高比约束。
  4. 这是我管理此问题的代码的复制粘贴(添加了注释)。希望它会对你有所帮助。

        // 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
                    }
                })
            }
        }
    

答案 1 :(得分:0)

首先将您的底部内容设置为这样的高度,

enter image description here

然后设置你的图像约束,

enter image description here

然后在您的代码中执行此操作,

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

    }

你应该得到这样的事情, enter image description here

相关问题