单元格中的圆形图像以及如何调整大小? [英] circle image in cell imageView and how to resize it?

查看:126
本文介绍了单元格中的圆形图像以及如何调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ViewController tableView中有标识符Cell。这是我的代码:

I have in a ViewController tableView with the identifier "Cell". This is my code:

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 )高度等于行高度,我不能改变这个细胞图像的高度和宽度。

So, cornerRadius works, but one of the problem is that my image in the cell(cell.imageView?.image) height is equal to row height and I cannot change neither height, nor width of this cell image.

当我尝试

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

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

nothing changes. Who knows, what is the problem?

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

And as I cannot control my image height and width I get as result rectangle with the border radius, but I want to have a square(w: 100, h: 100).

推荐答案

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

You can use this extension to crop the longest side of your image and return a squared 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
    }

}

这篇关于单元格中的圆形图像以及如何调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆