我想在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
}
我在这里填写tableView时也尝试设置cornerRadius,但是我得到了相同的结果。我也尝试过所有的contentMode模式(AspecFill,ScaleToFill ..) 奇怪的是,在另一个VC中,我有相同的布局,而这种情况并没有发生。
答案 0 :(得分:0)
我有像你这样的确切问题,我尝试在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
}