无法使角半径工作

Can't get corner radius to work

您好,我正在编写一个聊天应用程序,我不希望个人资料图片有圆角半径。这是我的代码。顺便说一句,我使用 Parse 作为后端。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    var imageView = PFImageView()
    let user = self.users[indexPath.row]
    imageView.layer.cornerRadius = imageView.frame.size.width / 2
    imageView.layer.masksToBounds = true
    imageView.file = user[PF_USER_PICTURE] as? PFFile
    cell.textLabel?.text = user[PF_USER_FULLNAME] as? String
    cell.textLabel?.font = UIFont.boldSystemFontOfSize(24)
    cell.imageView?.image = imageView.image



    return cell
}

更新: 问题是我的 cell.imageview 没有框架。 感谢 Max K 的回答!

var imageView = PFImageView()

与以下图片视图不同:

cell.imageView?.image = imageView.image

屏幕上显示的是您的单元格的图像视图,而不是 PFImageView 的这个实例。如果您想将 PFImageView 作为单元格图像视图的 class,您需要在情节提要中或通过子 class 单元格进行设置。

您正在 imageView 上设置 cornerRadius,稍后不会添加到屏幕。

您必须在 cell.imageView

上设置它
if let imageView = cell.imageView {
    imageView.layer.cornerRadius = imageView.frame.size.width / 2
}

您将 cornerRadius 应用于此处创建的 imageView 图层:

var imageView = PFImageView()

但是你只使用它的 image 属性 并将它分配给单元格的 imageView:

cell.imageView?.image = imageView.image

cornerRadius 应用于单元格的 imageView

您正在向

中的 imageView 添加角

var imageView = PFImageView()

改为在 cell.imageView 上添加角。看起来您正在使用 PFImageView 从文件下载图像。所以你可以像

这样修改代码
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

var imageView = PFImageView()
let user = self.users[indexPath.row]
cell.imageView.layer.cornerRadius = imageView.frame.size.width / 2
cell.imageView.layer.masksToBounds = true
imageView.file = user[PF_USER_PICTURE] as? PFFile
cell.textLabel?.text = user[PF_USER_FULLNAME] as? String
cell.textLabel?.font = UIFont.boldSystemFontOfSize(24)
cell.imageView?.image = imageView.image

return cell
}

否则将 cell.imageView 的 imageView 设为 PFImageView