iPhone : Getting the size of an image after AspectFt

前端 未结 4 1603
天涯浪人
天涯浪人 2020-12-28 15:04

Real odd one to get stuck on but weirdly I am.

You you have a imageView containing a image. You size that imageView down and then tell it

4条回答
  •  抹茶落季
    2020-12-28 15:18

    Just adding a little security on Individual11 code. Calling imageScale on an imageView without an actual image would crash.

    extension UIImageView {
        var imageScale: CGSize {
    
            if let image = self.image {
                let sx = Double(self.frame.size.width / image.size.width)
                let sy = Double(self.frame.size.height / image.size.height)
                var s = 1.0
                switch (self.contentMode) {
                case .scaleAspectFit:
                    s = fmin(sx, sy)
                    return CGSize (width: s, height: s)
    
                case .scaleAspectFill:
                    s = fmax(sx, sy)
                    return CGSize(width:s, height:s)
    
                case .scaleToFill:
                    return CGSize(width:sx, height:sy)
    
                default:
                    return CGSize(width:s, height:s)
                }
            }
    
            return CGSize.zero
        }
    }
    

提交回复
热议问题