我想要一个圆形的图像在一个UIImageView。我对UITableViewCell进行子类化,并在awakeFromNib中添加了cornerRadious,如下所示:
class FriendsCell: UITableViewCell {
@IBOutlet weak var friendImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
self.friendImageView.layer.cornerRadius = self.friendImageView.frame.size.width / 2
self.friendImageView.clipsToBounds = true
}我得到的是:

这正是我所期望的:

填充tableView的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as FriendsCell
let user = fetchedResultsController.objectAtIndexPath(indexPath) as Friend
cell.friendImageView.image = UIImage(data: user.photo)
return cell
}在这里填充cornerRadius时,我也尝试过设置tableView,但是我得到了相同的结果。我还尝试了contentMode的所有模式(AspecFill,ScaleToFill.)奇怪的是,在另一个VC,我有相同的布局,这是不可能发生的。
发布于 2015-08-08 19:13:51
我有和您一样的问题,我尝试在viewWillLayoutSubviews()函数中设置这个问题。然后一切都好起来了!下面是我的代码
override func viewWillLayoutSubviews() {
detailPortImageView.image = detailImage
detailPortImageView.layer.cornerRadius = detailPortImageView.frame.width / 2
detailPortImageView.clipsToBounds = true
detailPortImageView.layer.borderWidth = 2
detailPortImageView.layer.borderColor = UIColor.whiteColor().CGColor
}https://stackoverflow.com/questions/27768271
复制相似问题