我有这个 UIImage 的扩展:
extension UIImage {
func resizeImage(newWidth: CGFloat, newHeight: CGFloat? = nil) -> UIImage {
let scale = newWidth / self.size.width
let finalHeight = (newHeight == nil) ? self.size.height * scale : newHeight!
UIGraphicsBeginImageContextWithOptions(CGSize(width:newWidth, height: finalHeight), false, self.scale)
self.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: finalHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
Run Code Online (Sandbox Code Playgroud)
为什么 UIGraphicsGetImageFromCurrentImageContext 返回 nil?跟图片大小有关系吗?
(lldb) po self.size ?(3024.0, 4032.0) - 宽度:3024.0 - 高度:4032.0