How do set a width and height of an Image in Swift

后端 未结 4 1189
慢半拍i
慢半拍i 2020-12-08 04:46

New to Swift and iOS programming in general. I couldn\'t figure out how to set a width and height of an image.
For example,

  l         


        
4条回答
  •  执笔经年
    2020-12-08 05:28

    Swift 3.0 version (from @YannickSteph)

    extension UIImage {
    
        func imageResize (sizeChange:CGSize)-> UIImage{
    
            let hasAlpha = true
            let scale: CGFloat = 0.0 // Use scale factor of main screen
    
            UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
            self.draw(in: CGRect(origin: CGPoint.zero, size: sizeChange))
    
            let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
            return scaledImage!
        }
    
    }
    

提交回复
热议问题