Обрезать по кругу cell.imageView? - PullRequest
0 голосов
/ 26 октября 2019

Не могу обрезать cell.imageView? в кружок.

Я был на многих форумах и ничего не нашел.

Я использую tableView со стандартной ячейкой (нестандартно).

Мой код:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "channelCell", for: indexPath) 
    cell.accessoryType = .disclosureIndicator
    cell.imageView?.layer.cornerRadius = cell.frame.size.height / 2
    cell.imageView?.clipsToBounds = true
    cell.imageView?.kf.setImage(with: URL(string: channels[indexPath.row].userUrlImage))
    return cell
}

Я получаю это изображение, проверьте изображение на втором пользователе

enter image description here

UPD.

enter image description here

Ответы [ 2 ]

0 голосов
/ 26 октября 2019

В то время, когда вы заполняете свою ячейку, ваша ячейка не размещает в ней кадры.
1) Один из подходов состоит в том, чтобы вызвать layoutIfNeeded () и затем установить угловой радиус, равный cell.imageView? .Layer. cornerRadius = cell.imageView? .bounds.frame.height / 2 Но это не лучше с точки зрения производительности, потому что вы создаете избыточный проход макета.
2) Другой подход - создать подкласс UITableViewCell и переопределить его layoutSubviews,Затем в layoutSubviews установите угловой радиус.
3) Самый простой и лучший с точки зрения возможности повторного использования объект - создать подкласс UIImageView и переопределить подпредставления макета. Вот код для третьего подхода

/*
Create custom UITableViewCell and use CircledImageView instead of UIImageView.
*/
class CircledImageView: UIImageView {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.layer.cornerRadius = self.bounds.size.height / 2
    }
}
0 голосов
/ 26 октября 2019

Вы должны установить рамку для просмотра изображения до:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "channelCell", for: indexPath) 
    cell.accessoryType = .disclosureIndicator
    cell.imageView?.frame = CGRect(x: 0, y: 0, width: cell.frame.size.height, height: cell.frame.size.height)
    cell.imageView?.contentMode = .scaleToFill
    cell.imageView?.layer.cornerRadius = cell.frame.size.height / 2
    cell.imageView?.clipsToBounds = true
    cell.imageView?.kf.setImage(with: URL(string: channels[indexPath.row].userUrlImage))
    return cell
}
...