In Xcode 6.1. 'UIImage?' does not have a member named 'size' Error

前端 未结 4 960
既然无缘
既然无缘 2020-12-04 03:36

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\

相关标签:
4条回答
  • 2020-12-04 03:45

    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
    
    0 讨论(0)
  • 2020-12-04 03:54

    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
    }
    
    0 讨论(0)
  • 2020-12-04 03:59

    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!

    0 讨论(0)
  • 2020-12-04 04:00

    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!)
    
    0 讨论(0)
提交回复
热议问题