0

I am trying to shrink the size of the icons that are displayed in each cell. The code I am trying to use is:

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
let cellIcon = UIImage(named: tableIcons[indexPath.row])
cell.imageView!.frame = CGRect(x: cell.imageView!.frame.origin.x, y: cell.imageView!.frame.origin.y, width: cell.imageView!.frame.size.width / 2, height: cell.imageView!.frame.size.height / 2)
cell.imageView!.image = cellIcon
cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFill
return cell

I would have thought this would shrink the imageview by half and have the image scale to fill the now halved view. But the image in the cells stay the same size. Am I going about this problem correctly?

Tamarisk
  • 529
  • 3
  • 6
  • 15
  • 2
    In my experience, if you use the builtin views (imageView, textLabel, detailTextLabel) and try doing anything fancy to change their sizes, you are in infinite trouble. – gnasher729 May 22 '15 at 01:00
  • If the imageview is meant to be half of the current size, I think you should set the width and height to half of it at the first place. Also, you can try set cell.imageView?.clipsToBound = true – L.C. Tan May 22 '15 at 01:16

1 Answers1

0

There is a function to resize an image in the SO referenced below which I have used and it has worked well to create thumbnails from a full image. I have shown the function below: resize image

    func imageResize (imageObj:UIImage, sizeChange:CGSize)-> UIImage{

        let hasAlpha = false
        let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

        UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
        imageObj.drawInRect(CGRect(origin: CGPointZero, size: sizeChange))

        let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
        return scaledImage
}
Community
  • 1
  • 1
Syed Tariq
  • 2,878
  • 3
  • 27
  • 37