shot shot - 4 months ago 31
Swift Question

CGSize width and height print as zero

I want to know the width and height of image, but CGSize always returns 0.

let img : UIImage = UIImage.init(named: "icon_ear")!
NSLog("img w : %d, h : %d", img.size.width, img.size.height)
//img w : 0, h : 0 `


and

let cs = CGSize(width: 100, height: 100)
NSLog("cs w : %d, h : %d", cs.width, cs.height)
//cs w : 0, h : 0


What is wrong?

Answer

CGSize's width and height are floating point and not integer. Just change the format specifier from %d to %f.

NSLog("cs w : %f, h : %f", cs.width, cs.height)
Comments