shot shot - 1 year ago 130
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 `


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 Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download