简体   繁体   English

表格视图单元格中的情节提要板图像视图

[英]Storyboard imageview in a tableview cell

I have an imageview inside a tableview cell of tableview. 我在tableview的tableview单元格内有一个imageview。 what i want to achieve is it should resize the imageview according to the image it contain dynamically.Currently I have it like this: 我想要实现的是它应该根据它包含的图像动态调整imageview的大小。

在此处输入图片说明

I had this same exact issue not long ago. 我不久前也遇到了同样的问题。

First, you should set image content mode to Aspect Fit, BUT it is not enough. 首先,您应该将图像内容模式设置为Aspect Fit, 但是这还不够。

You will have to change imageView's aspect ratio constraint every time you want to load a new image. 每次要加载新图像时,都必须更改imageView的宽高比约束。 Basically, what you need to do is: 基本上,您需要做的是:

  1. Get your image's width and height 获取图像的宽度和高度
  2. Calculate aspect ratio let aspectRatio = Float(image.height) / Float(image.width) 计算长宽比let aspectRatio = Float(image.height) / Float(image.width)
  3. Use that aspect ratio to create a new aspect ratio constraint for your image view. 使用该纵横比可以为图像视图创建新的纵横比约束。

This is a copy-paste (with added comments) of my code where I managed this issue. 这是我管理此问题的代码的复制粘贴(带有添加的注释)。 Hope it will help you. 希望对您有帮助。

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

First set your bottom content a height like this, 首先将您的底部内容设置为这样的高度,

在此处输入图片说明

Then set your image constrains like this, 然后像这样设置图像约束

在此处输入图片说明

Then do this in your code, 然后在您的代码中执行此操作

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

    }

You should get somthing like this, 你应该得到这样的东西 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM