【问题标题】:circle image in cell imageView and how to resize it?单元格imageView中的圆形图像以及如何调整它的大小?
【发布时间】:2015-06-14 09:10:33
【问题描述】:

我有一个带有标识符“Cell”的 ViewController tableView。这是我的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    var url = NSURL(string: postCover[indexPath.row])
    let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check

    cell.imageView?.contentMode = .ScaleAspectFit
    cell.imageView?.clipsToBounds = true

    cell.imageView?.image = UIImage(data: data!)
    cell.textLabel?.text = postTitle[indexPath.row]

    cell.imageView?.layer.cornerRadius =  20
    cell.imageView?.layer.masksToBounds = true;

    return cell
}

所以,cornerRadius 有效,但问题之一是我在单元格中的图像 (cell.imageView?.image) 高度等于行高,我无法更改此单元格图像的高度和宽度。

当我尝试时

cell.imageView?.frame.size.width = 100

没有任何变化。谁知道,有什么问题?

由于我无法控制图像的高度和宽度,我得到的结果是带有边框半径的矩形,但我想要一个正方形(w:100,h:100)。

【问题讨论】:

  • 你到底想要什么,圆形图像还是正方形?
  • 尝试使用heightForRowAtIndexPath
  • @AnilVarghese 通告。我确实解释过,现在我得到了带有边框半径的矩形。所以我必须有正方形才能使用cornerRadius并获得圆形图像
  • @VNJ 不,我已经设置了行高。我想要例如:我的行高度 = 100,但单元格内的图像高度和宽度将:高度:80,宽度:80
  • 能不能看一下屏幕截图,让大家有个清晰的思路?

标签: ios uitableview swift


【解决方案1】:

您可以使用此扩展来裁剪图像的最长边并返回一个平方的 UIImage:

extension UIImage {
    var squared: 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
        UIGraphicsBeginImageContext(imageView.bounds.size)
        imageView.layer.renderInContext(UIGraphicsGetCurrentContext())
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }
    var circle: 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.masksToBounds = true
        UIGraphicsBeginImageContext(imageView.bounds.size)
        imageView.layer.renderInContext(UIGraphicsGetCurrentContext())
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }
    func resizeToWidth(width:Int)-> UIImage {
        let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: CGFloat(width), height: CGFloat(ceil(CGFloat(width)/size.width * size.height)))))
        imageView.contentMode = UIViewContentMode.ScaleAspectFit
        imageView.image = self
        UIGraphicsBeginImageContext(imageView.bounds.size)
        imageView.layer.renderInContext(UIGraphicsGetCurrentContext())
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }

}

【讨论】:

  • 谢谢你,对不起,我该如何使用这个扩展程序?
  • yourImage.circle 或 yourImage.squared
  • 您也可以根据需要缩小尺寸。你希望你的图片缩略图是多少像素?
  • 我把它放在文件的底部,它会打印下一个错误:Declaration is only valid at file scope
  • yourImage.resizeToWidth(128).circle
【解决方案2】:

您不能单独更改框架的宽度或高度。您必须设置完整的框架。在 Objective C 中,它会类似于

cell.imageView.frame = CGRectMake(cell.imageView.frame.origin.x, cell.imageView.frame.origin.y, 100, 100);

【讨论】:

  • 但我是用 Swift 编写的。出现下一个错误:Value of optional type 'CGFloat?' not unwrapped; did you mean to use '!' or '?'?
  • 我无法在这里测试它,但它可能类似于: cell.imageView?.frame = CGRectMake(x:cell.imageView.frame.origin.x,y:cell.imageView.frame.origin .y,宽度:100,高度:100);
  • Value of optional type 'CGFloat?' not unwrapped; did you mean to use '!' or '?'?Extraneous argument labels 'x:y:width‌​:height:' in call 错误
【解决方案3】:

您不能更改任何视图的框架属性的属性。你必须提供一个全新的框架。

cell.imageView?.frame.size.width = 100 //wont work

尝试这样做 -

let rect = cell.imageView?.frame;

rect.size.width = 100;

cell.imageView?.frame = rect; //this will work

【讨论】:

  • 'CGRect?' does not have a member named 'size' 第二行出现错误
  • 它因我在上面的评论中发布的错误而失败
  • 这怎么可能? CGrect 有成员大小!!!在这里查看developer.apple.com/library/ios/documentation/GraphicsImaging/…
  • 我不知道 man =/ 我使用的不是 TableViewController,而是 ViewController,只是为了了解信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 2010-10-27
  • 1970-01-01
  • 2019-06-21
  • 2015-03-09
  • 2014-10-28
相关资源
最近更新 更多