如何在Swift的tableview单元格中添加自定义imageview?

时间:2015-05-05 04:41:56

标签: ios iphone uitableview swift cell

我需要做的是在uitableview上将圆形图像添加到单元格。我该怎么做?

我尝试了一些代码,但我根本不工作,这是我的代码:

let imageName = "person.png"
    let image = UIImage(named: imageName)
    var imageView = UIImageView(image: image!)

    imageView = UIImageView(frame: CGRectMake(0, 0, 100, 100))
    imageView.layer.borderWidth=1.0
    imageView.layer.masksToBounds = false
    imageView.layer.borderColor = UIColor.whiteColor().CGColor
    imageView.layer.cornerRadius = 13;
    imageView.clipsToBounds = true

    cell.imageView!.image = imageView

我收到了这个错误:

  

不能将“UIImageView”类型的值赋给“UIImage”类型的值?'

5 个答案:

答案 0 :(得分:17)

您不能以这种方式分配Image。但你可以这样做。

首先添加将调整图像大小的功能:

func resizeImage(image:UIImage, toTheSize size:CGSize)->UIImage{


    var scale = CGFloat(max(size.width/image.size.width,
        size.height/image.size.height))
    var width:CGFloat  = image.size.width * scale
    var height:CGFloat = image.size.height * scale;

    var rr:CGRect = CGRectMake( 0, 0, width, height);

    UIGraphicsBeginImageContextWithOptions(size, false, 0);
    image.drawInRect(rr)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext();
    return newImage
}

之后你可以分配图像:

let imageName = "11749-simple.jpg"
let image = UIImage(named: imageName)
let newImage = resizeImage(image!, toTheSize: CGSizeMake(70, 70))
var cellImageLayer: CALayer?  = cell.cellImage.layer
cellImageLayer!.cornerRadius = cellImageLayer!.frame.size.width / 2
cellImageLayer!.masksToBounds = true
cell.cellImage.image = newImage

结果将是:

enter image description here

或者你可以试试这个扩展名:

extension UIImage {
var circleMask: UIImage {
    let square = size.width < size.height ? CGSize(width: size.width, height: size.width) : CGSize(width: size.height, height: size.height)
    let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: square))
    imageView.contentMode = UIViewContentMode.ScaleAspectFill
    imageView.image = self
    imageView.layer.cornerRadius = square.width/2
    imageView.layer.borderColor = UIColor.whiteColor().CGColor
    imageView.layer.borderWidth = 5
    imageView.layer.masksToBounds = true
    UIGraphicsBeginImageContext(imageView.bounds.size)
    imageView.layer.renderInContext(UIGraphicsGetCurrentContext())
    let result = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return result
    }
}

您可以指定图片:

let imageName = "11749-simple.jpg"
let image = UIImage(named: imageName)
cell.cellImage.image = image?.circleMask

延长信用额度THIS回答。

答案 1 :(得分:1)

制作一个Cell类,希望加载TableView

班级CellComment

class CellComment: UITableViewCell 
{
  @IBOutlet weak var imageV_profile: UIImageView!

    override func awakeFromNib() {

          super.awakeFromNib()

          imageV_profile.layer.cornerRadius = 25

         imageV_profile.layer.masksToBounds = true
    }
}

现在在tableview中加载此单元格,如下所示。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

       var  cell:CellPost! = tableView.dequeueReusableCellWithIdentifier("CellPost") as? CellPost

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

      cell.imageV_profile.image = UIImage(named: "Your image name")

      return cell
}

答案 2 :(得分:1)

试试这段代码。这给出了你想要的......

let imageName = "person.png"
let image1 = UIImage(named: imageName)

var imageView = UIImageView(frame: CGRectMake(5, 5, 100, 100))
imageView.layer.borderWidth=1.0
imageView.layer.masksToBounds = true
imageView.layer.borderColor = UIColor.whiteColor().CGColor
imageView.layer.cornerRadius = 50;// Corner radius should be half of the height and width.
imageView.image = image1
cell.addSubview(imageView)

答案 3 :(得分:1)

 can you just replace imageView to image 

let imageName = "person.png"
let image = UIImage(named: imageName)
var imageView = UIImageView(image: image!)
imageView = UIImageView(frame: CGRectMake(0, 0, 100, 100))
imageView.layer.borderWidth=1.0
imageView.layer.masksToBounds = false
imageView.layer.borderColor = UIColor.whiteColor().CGColor
imageView.layer.cornerRadius = 13;
imageView.clipsToBounds = true

cell.imageView!.image = image 

答案 4 :(得分:1)

Swift 5的扩展

extension UIImage {
var circleMask: UIImage {
    let square = size.width < size.height ? CGSize(width: size.width, height: size.width) : CGSize(width: size.height, height: size.height)
    let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: square))
    imageView.contentMode = UIView.ContentMode.scaleAspectFill
    imageView.image = self
    imageView.layer.cornerRadius = square.width/2
    imageView.layer.borderColor = UIColor.white.cgColor
    imageView.layer.borderWidth = 5
    imageView.layer.masksToBounds = true
    UIGraphicsBeginImageContext(imageView.bounds.size)
    imageView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let result = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return result
    }
}