移动端开发

​swift修改图片UIImage宽度并高度自适应

内容

swift -> 修改图片UIImage宽度, 并高度自适应



func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {  

     

   let scale = newWidth / image.size.width  

   let newHeight = image.size.height * scale  

   UIGraphicsBeginImageContext(CGSize(width:newWidth, height:newHeight))  

   image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))  

   let newImage = UIGraphicsGetImageFromCurrentImageContext()  

   UIGraphicsEndImageContext()  

     

   return newImage!  

}

标签

​swift

|{ value.name }