自定义表格视图单元格图像扭曲

时间:2014-11-06 13:11:48

标签: ios uitableview swift

我有一个自定义的UITableViewCell,我也有一个图像,但图像拉伸相当不寻常。这是代码:

    var count = 0
    var propic: NSString = ""
    var name: NSString = ""
    for data in listDataArray {
        if(indexPath.row == count) {
            if let pic = data["profile_pic"] as? NSString {
                propic = pic
            }
            if let n = data["full_name"] as? NSString {
                name = n
            }
        }
        count++
    }
    let decodedData = NSData(base64EncodedString: propic, options: NSDataBase64DecodingOptions(0))
    var decodedimage = UIImage(data: decodedData!)
    var  cell:TableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell") as? TableViewCell

    if (cell == nil)
    {
        let nib:Array = NSBundle.mainBundle().loadNibNamed("TableViewCell", owner: self, options: nil)
        cell = nib[0] as? TableViewCell
    }

    cell!.nameLabel!.text = name
    cell!.imageView!.image = decodedimage
    cell!.imageView!.layer.cornerRadius = cell!.recipeImageView!.frame.size.width / 2
    cell!.imageView!.layer.borderWidth = 5.0
    cell!.imageView!.clipsToBounds = true
    cell!.imageView!.layer.borderColor = UIColor(red: 128/225.0, green: 188/225.0, blue: 163/225.0, alpha: 0.45).CGColor;
    return cell;

这是.xib

xib

TableViewCell.swift文件:

@IBOutlet var imageView : UIImageView? = UIImageView(frame: CGRectMake(10, 10, 50, 50))
@IBOutlet var nameLabel : UILabel?
@IBOutlet var timeLabel : UILabel?

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

和模拟器中的结果:

result

我需要将图像限制在一个块(并在一个圆圈中)以显示如下:

image

除了是方形图像之外,我需要它们成为圆形。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以使用Cocoapods将此库添加到项目中 platform :ios pod 'APAvatarImageView'

并将类APAvatarImageView添加到Storyboard或XIB中的UIImageView,它将自动成为一个圆圈https://github.com/ankurp/APAvatarImageView