Greg Peckory
Greg Peckory

Reputation: 8068

Changing image properties UITableView Swift

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

    let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell

    cell.imageView?.image = nil
    let imgURL = NSURL(string: urlString)

    cell.imageView?.image = UIImage(named: "first")

    // cell.imageView?.image?.drawInRect(CGRectMake(0,0,50,50))
    // or
    // cell.imageView?.frame = CGRectMake(0,0,50,50)

    // also tried this


    cell.imageView?.image = UIImage(named: "first")  
    cell.imageView?.contentMode = .ScaleAspectFill
    cell.imageView?.clipsToBounds = true

Neither seems to make a difference.

When the image loads from the URL online it takes its original size.

I have omitted the code used for asynchronous image downloading because I figure its the same problem here. If not let me know. Thanks !

Upvotes: 1

Views: 632

Answers (2)

Hugo Alonso
Hugo Alonso

Reputation: 6824

You should set Mode property of your UIImageView into Aspect Fit where you are showing the image at your cell.

Upvotes: 1

pbush25
pbush25

Reputation: 5248

You need to set the contentmode of the imageView either in code or in Interface Builder, and then once you do that you need to usecell.imageView?.clipsToBounds = true

Upvotes: 0

Related Questions