circle image in cell imageView and how to resize it?

后端 未结 3 899
孤街浪徒
孤街浪徒 2021-01-29 00:21

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSInd         


        
相关标签:
3条回答
  • 2021-01-29 00:37

    You cannot change an attribute of the frame property of any view. You have to provide a totally new frame.

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

    Try doing this -

    let rect = cell.imageView?.frame;
    
    rect.size.width = 100;
    
    cell.imageView?.frame = rect; //this will work
    
    0 讨论(0)
  • 2021-01-29 00:48

    You cannot change a frame width or height independently. You have to set the complete frame. In Objective C it would be something like

    cell.imageView.frame = CGRectMake(cell.imageView.frame.origin.x, cell.imageView.frame.origin.y, 100, 100);
    
    0 讨论(0)
  • 2021-01-29 00:54

    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
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题