Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使UIImage width等于view';自定义单元格内的宽度和比例高度_Ios_Swift_Uitableview_Uiimageview_Uiimage - Fatal编程技术网

Ios 如何使UIImage width等于view';自定义单元格内的宽度和比例高度

Ios 如何使UIImage width等于view';自定义单元格内的宽度和比例高度,ios,swift,uitableview,uiimageview,uiimage,Ios,Swift,Uitableview,Uiimageview,Uiimage,我正在尝试下载一个图像并将其放置在自定义单元格中,其中包含UIImage和UITextView。我需要它等于屏幕的宽度和高度成比例,类似于此,但我不知道如何使用这个转换的答案使用自定义表格单元格。尝试在表格视图中设置图像宽度和高度时:cellForRowAt调用cell.storedImage.frame.width和heightXcode表示它只是一个get only属性。这就是我认为有效的方法 override func tableView(_ tableView: UITableView,

我正在尝试下载一个图像并将其放置在自定义单元格中,其中包含UIImage和UITextView。我需要它等于屏幕的宽度和高度成比例,类似于此,但我不知道如何使用这个转换的答案使用自定义表格单元格。尝试在
表格视图中设置图像宽度和高度时:cellForRowAt
调用
cell.storedImage.frame.width
height
Xcode表示它只是一个
get only
属性。这就是我认为有效的方法

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableData[indexPath.row]["Image"] != nil {
        let cell = tableView.dequeueReusableCell(withIdentifier: "imageNotesData", for: indexPath) as! ImageNotesCell
        cell.notes.delegate = self
        cell.notes.tag = indexPath.row
        cell.notes.text = tableData[indexPath.row]["Notes"] as! String
        guard let imageFirebasePath = tableData[indexPath.row]["Image"] else {
            return cell }
        let pathReference = Storage.storage().reference(withPath: imageFirebasePath as! String)
        pathReference.getData(maxSize: 1 * 1614 * 1614) { data, error in
            if let error = error {
                print(error)
            } else {
                //let image = UIImage(data: data!)
                //cell.storedImage.image = image


                let containerView = UIView(frame: CGRect(x:0,y:0,width:self.view.frame.width
                    ,height:500))
                let imageView = UIImageView()
                if let image = UIImage(data:data!) {
                    let ratio = image.size.width / image.size.height
                    if containerView.frame.width > containerView.frame.height {
                        let newHeight = containerView.frame.width / ratio
                        imageView.frame.size = CGSize(width: containerView.frame.width, height: newHeight)
                        cell.storedImage.frame.height = newHeight
                    }
                    else{
                        let newWidth = containerView.frame.height * ratio
                        imageView.frame.size = CGSize(width: newWidth, height: containerView.frame.height)
                        cell.storedImage.frame.width = newWidth
                    }
                }
                let image = UIImage(data: data!)
                cell.storedImage.image = image
            }
        }
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "notesData", for: indexPath) as! NotesCell
        //let noteString = tableData[indexPath.row]["Notes"] as! String
        cell.notes.text = tableData[indexPath.row]["Notes"] as! String
        cell.notes.delegate = self
        cell.notes.tag = indexPath.row
        return cell
    }
}
我还尝试使用自动布局来解决这个问题,方法是让下面的文本视图有两个不同的底部边距限制,但它看起来仍然不正确。这是我用自动布局约束得到的壁橱。当前,图像视图的每一侧都有0个空间约束,以创建超级视图


我假设代码片段是我应该使用的路径,但我不确定如何使图像看起来正确

您仍然可以使用动态约束,一旦获得图像,您就可以为uiimageview设置纵横比约束,但这样做可能会更麻烦

您也可以直接对框架执行此操作,但是您需要为框架创建一个新的CGRect,您不能简单地直接编辑它的宽度或高度

您还应该将行的高度设置为正确

我假设您已经获得了ImageNotesCell在初始化器中设置了默认大小,并带有注释某种类型的uitextfield/uitextview继承类和storedImage只是一个图像

class ImageNotesCell: UITableViewCell {
  func setImage(_ image: UIImage) {
    let previousNotesFrame = notes.frame
    let ratio = image.size.height / image.size.width
    storedImage.image = image
    let newImageFrame = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.width * ratio)
    let newNotesFrame = CGRect(x: 0, y: newImageFrame.size.height + your_spacing_here, width: frame.size.width, height: previousNotesFrame.size.height)
    frame = CGRect(x: 0, y: 0, width: frame.size.width, height: newImageFrame.size.height + your_spacing_here + newNotesFrame.size.height)
    storedImage.frame = newImageFrame
    notes.frame = newNotesFrame
  }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableData[indexPath.row]["Image"] != nil {
        let cell = tableView.dequeueReusableCell(withIdentifier: "imageNotesData", for: indexPath) as! ImageNotesCell
        cell.notes.delegate = self
        cell.notes.tag = indexPath.row
        cell.notes.text = tableData[indexPath.row]["Notes"] as! String
        guard let imageFirebasePath = tableData[indexPath.row]["Image"] else {
            return cell }
        let pathReference = Storage.storage().reference(withPath: imageFirebasePath as! String)
        pathReference.getData(maxSize: 1 * 1614 * 1614) { data, error in
            if let error = error {
                print(error)
            } else {
                let image = UIImage(data: data!)
                cell.setImage(image)
            }
        }
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "notesData", for: indexPath) as! NotesCell
        //let noteString = tableData[indexPath.row]["Notes"] as! String
        cell.notes.text = tableData[indexPath.row]["Notes"] as! String
        cell.notes.delegate = self
        cell.notes.tag = indexPath.row
        return cell
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if tableData[indexPath.row]["Image"] != nil {
        guard let imageFirebasePath = tableData[indexPath.row]["Image"] else {
            return your_default_height_here }
        let pathReference = Storage.storage().reference(withPath: imageFirebasePath as! String)
        pathReference.getData(maxSize: 1 * 1614 * 1614) { data, error in
            if let error = error {
                print(error)
            } else {
                let image = UIImage(data: data!)
                let ratio = image.size.height / image.size.width
                return (tableView.frame.size.width * ratio) + your_spacing_value_here + your_notes_height_here
            }
        }
    } else {
        return your_notes_cell_height_here
    }
}

我在自己制作的自定义UITableViewCell上测试了逻辑,并使用了3种不同的图像尺寸和注释。它填满了屏幕。但是,由于我不知道你在想什么,所以我将把它留给你去适应,但这里是它的要点:

ImageNotesCell类:

  • 添加一个函数,将图像设置到单元格中,从而更新单元格、注释和图像的帧
控制器类:

  • cellForRow:初始化你的单元格,像往常一样为notes部分设置,当没有任何图像时,调用setImage方法,返回你的单元格
  • heightForRow:返回没有要加载的图像时的通常高度,获取图像,计算其高宽比,乘以tableView宽度,添加间距值,添加注释高度

更新了有关
cellForRowAt的更多信息
你解决了这个问题吗?没有,我刚刚放弃了它。请尽快告诉我这个问题。你需要图像和标签之间的间隙吗?@LampPost由于我的回答或其他原因,你解决了你的问题吗?如果你自己找到了答案,请随意分享,并将其作为答案,你永远不知道谁会来寻找它,或者我的答案是否帮助你随意标记它为正确。