如果我使用圆形图像,图像不会显示在 imageView 中

Image not showing in imageView if I used circular image

我有一个 table 视图,我需要在 header 部分显示 author_img。在 ViewForSectionHeader 方法中,我想让图像成为圆形。但如果我这样做了,无论是在模拟器中还是在真实设备中,图像都不会显示。如果我删除代码,uiimageview 将正常显示图像。我为 uiimageview 设置了宽度和高度约束,因为我想获得显示图像的固定大小。那么有什么想法吗?

    cell.author_img.layer.cornerRadius =  cell.author_img.frame.size.width/2
    cell.author_img.layer.masksToBounds = true
    //  add a boundary for thumb
    cell.author_img.layer.borderWidth = 1.5
    cell.author_img.layer.borderColor = UIColor.whiteColor().CGColor

可能是因为你设置cornerRadius太早了,你的author_img最终大小还没有确定。 当您确定您的单元格的布局已被完全计算时,尝试设置该 cornerRadius。

出于测试目的,只是为了确保它来自此行为,请尝试对 cornerRadius 进行硬编码。

cell.author_img.layer.cornerRadius = 10

如果你的图片有轻微的cornerRadius,这意味着问题出在我之前解释的地方,如果仍然没有显示,那就是其他问题。

如果确实是因为您的图像大小不固定,请尝试将您的 cornerRadius 设置为 viewDidAppear,我的意思是您可以做的最新的事情。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerView : UIView = UIView.init(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
        headerView.backgroundColor = UIColor(red: 75/255.0, green: 193/255.0, blue: 210/255.0, alpha: 1.0)
        let iconimage: UIImageView = UIImageView(frame: CGRectMake(10, 0, 40, 40))
        iconimage.image = UIImage(named: "original.jpg")
        iconimage.layer.cornerRadius = iconimage.frame.size.height / 2
        iconimage.layer.borderColor = UIColor.whiteColor().CGColor
        iconimage.layer.borderWidth = 1.0
        iconimage.clipsToBounds = true

        let subjectNameLabel = UILabel(frame: CGRect(x: 60, y: 4, width: 250, height: 35))
        subjectNameLabel.textAlignment = NSTextAlignment.Center
        subjectNameLabel.font = UIFont (name: "HelveticaNeue", size: 16)
        subjectNameLabel.textColor = UIColor.whiteColor()
        headerView.userInteractionEnabled = true

        if isFristtime == true {
            let subjectNameTxtValue = "Mile Ho Tum Humko"
             subjectNameLabel.text = subjectNameTxtValue
        }else{
            let subjectNameTxtValue = "Saiyaan"
             subjectNameLabel.text = subjectNameTxtValue
        }




        subjectNameLabel.backgroundColor = UIColor(red: 75/255.0, green: 193/255.0, blue: 210/255.0, alpha: 1.0)
          headerView.addSubview(iconimage)
        headerView.addSubview(subjectNameLabel)

        return headerView
    }


func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }