Image not showing in imageView if I used circular image

本小妞迷上赌 提交于 2019-12-02 10:11:29

问题


I have a table view and I need to show author_img in section header. In ViewForSectionHeader method, I want to make the image to be circular. But If I did that, the images are not showing at all, no matter in simulator or in real device. If I remove the code, uiimageview will show the images normally. I set up the width and height constraint for uiimageview because I want to get fixed size of showing images. So Any ideas, please?

    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

回答1:


It may come from the fact that you are setting the cornerRadius too early, the final size of your author_img is not fixed yet. Try to set that cornerRadius when you are sure that the layout of your cell has been entirely calculated.

For test purpose, just to make sure it is coming from this behaviour, try to hardcode the cornerRadius.

cell.author_img.layer.cornerRadius = 10

If your image has a slight cornerRadius, it means that the issue is coming from what I explained earlier, if it is still not showing, it is coming from something else.

If it does come from the fact the size of your image is not fixed, try to set your cornerRadius in something like viewDidAppear, I mean the latest you can do.




回答2:


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
    }



来源:https://stackoverflow.com/questions/39888806/image-not-showing-in-imageview-if-i-used-circular-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!