I\'m getting an image\'s Size
and used the below code. It was working perfectly fine with Xcode 6.0.1
. After updating to Xcode 6.1
, I\
In addition to @Antonio's answer you can use nil coalescing operator(??) to get a default size in case image initializer is failed. Here is how:
let size = image?.size ?? CGSizeZero
That initializer is now a failable initializer, and as such it returns an optional UIImage
.
To quickly fix your bug just unwrap the image:
let sizeOfImage = image?.size
but I presume you will reference the image
variable more than just once in your code, in that case I suggest using optional binding:
if let image = image {
let sizeOfImage = image.size
/// ...
/// Use the image
}
Once you’re sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark (!) to the end of the optional’s name. In your case: image.size!
you can use like this:
let image = UIImage(named: "photo1.png")
let sizeOfImage = image?.size
imageView.image = image
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: sizeOfImage!)