Swift Circle图像视图

时间:2016-01-22 16:37:39

标签: ios swift imageview

我正在尝试为我的tableViewCell中的用户个人资料图片创建一个圆形imageView。我已经在我的代码中设置了约束,

        cell.creatorImageView = UIImageView()
        cell.creatorImageView?
            .translatesAutoresizingMaskIntoConstraints = false
        cell.creatorImageView!.image = UIImage(named: "memoryCardBack")
        cell.creatorImageView!.layer.cornerRadius =
            (cell.creatorImageView?.bounds.width)! / 2
        cell.creatorImageView?.clipsToBounds = true
        cell.profileCreatorView?.addSubview(cell.creatorImageView!)

        NSLayoutConstraint(item: cell.creatorImageView!, attribute: .Width,
            relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute,
            multiplier: 1, constant: 44).active = true

        NSLayoutConstraint(item: cell.creatorImageView!,
            attribute: .Height, relatedBy: .Equal, toItem: nil,
            attribute: .NotAnAttribute, multiplier: 1, constant: 44)
            .active = true

        NSLayoutConstraint(item: cell.creatorImageView!, attribute: .Top
            , relatedBy: .Equal, toItem: cell.profileCreatorView,
            attribute: .Top, multiplier: 1, constant: 5).active = true

        NSLayoutConstraint(item: cell.creatorImageView!,
            attribute: .CenterX, relatedBy: .Equal,
            toItem: cell.profileCreatorView, attribute: .CenterX,
            multiplier: 1, constant: 0).active = true

我不太清楚为什么imageView不会成为一个圆圈。

1 个答案:

答案 0 :(得分:2)

您可以使用CALayercornerRadius来执行此操作:

YourView.layer.cornerRadius = halfOfYourViewWidthOrHeight
YourView.clipsToBounds = true
YourView.contentMode = .ScaleAspectFill