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

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

问题描述:

我有一个自定义的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

只是除外的是方形图像,我需要它们是圆。我怎样才能做到这一点?

+0

你使用自动布局的圈?如果是的话,imageView有什么限制? – Tom 2014-11-06 13:17:52

+0

@Tom我没有使用自动布局 – Haring10 2014-11-06 13:21:05

+0

要将图像裁剪为圆形,可以在这里阅读: http://stackoverflow.com/questions/6530573/how-to-crop-uiimage-on-oval-shape-或圆形。 此外,要查看代码中的问题,我需要更多的见解。你能发给我(你的代码的一部分)吗?你指的是cell!.recipeImageView!我在你的发布代码中看不到任何地方。 – Tom 2014-11-06 13:25:31

可以将此库添加到使用的CocoaPods platform :ios pod 'APAvatarImageView'

项目和类APAvatarImageView添加到UIImageView的在你的故事板或XIB,它会自动https://github.com/ankurp/APAvatarImageView