Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a cell.imageView in a TableView and apply tintColor in Swift

I have a tableView with a cell created in cellForRowAtIndexPath and add some dummy text:

cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
            reuseIdentifier: "cell")
cell.textLabel?.text = "Test Title"
cell.detailTextLabel?.text = "Test detail label"

Then I add a test image to the cell's imageView:

var image = UIImage(named: "cd.png")
cell.imageView!.image = image

Result:

enter image description here

To adjust the color, I use the following code:

cell.imageView?.tintColor = UIColor.redColor()

Result:

Color adjusted cell imageView

The image is too big in the cell, so I adjust the size using the following code:

var itemSize:CGSize = CGSizeMake(20, 20)
UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.mainScreen().scale)
var imageRect : CGRect = CGRectMake(0, 0, itemSize.width, itemSize.height)
cell.imageView!.image?.drawInRect(imageRect)
cell.imageView!.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

Whilst the image does resize, the tintColor is lost:

enter image description here

Any ideas please?

like image 618
Anthony Avatar asked Jul 25 '15 08:07

Anthony


4 Answers

Solution Without custom cell

func imageWithImage(image: UIImage, scaledToSize newSize: CGSize) -> UIImage {
    
    UIGraphicsBeginImageContext(newSize)
    image.draw(in: CGRect(x: 0 ,y: 0 ,width: newSize.width ,height: newSize.height))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!.withRenderingMode(.alwaysOriginal) 
}



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

    cell.imageView?.tintColor = UIColor.green
    cell.imageView?.image = imageWithImage(UIImage(named: "imageName")!, scaledToSize: CGSize(width: 20, height: 20))
    
    // Configure the cell...
    
    return cell
  }
like image 62
Hamza Ansari Avatar answered Nov 03 '22 00:11

Hamza Ansari


You can do it by another way :

1) Create custom cell with your own UIImageView size and 2 separate labels

2) Add UIImageView as Subview of Cell

var cellImg : UIImageView = UIImageView(frame: CGRectMake(5, 5, 50, 50))
cellImg.image = UIImage(named: "yourimage.png")
cell.addSubview(cellImg)
like image 17
Ashish Kakkad Avatar answered Nov 02 '22 23:11

Ashish Kakkad


in swift 4 :

func image( _ image:UIImage, withSize newSize:CGSize) -> UIImage {

    UIGraphicsBeginImageContext(newSize)
    image.draw(in: CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!.withRenderingMode(.automatic)
}

cell.imageView?.image = image(UIImage(named: "yourImage.png")!, withSize: CGSize(width: 30, height: 30))
like image 8
soheil pakgohar Avatar answered Nov 03 '22 00:11

soheil pakgohar


For Swift 3

func imageWithImage(image:UIImage,scaledToSize newSize:CGSize)->UIImage{

        UIGraphicsBeginImageContext( newSize )
        image.draw(in: CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!.withRenderingMode(.alwaysTemplate)
    }

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

    cell.imageView?.tintColor = UIColor.greenColor()
    cell.imageView?.image = imageWithImage(image: UIImage(named: "imageName")!,scaledToSize: CGSize(width: 20, height: 20))

    // Configure the cell...

    return cell
  }
like image 6
Kushal Shrestha Avatar answered Nov 03 '22 01:11

Kushal Shrestha