【发布时间】:2017-09-07 15:35:22
【问题描述】:
我有这个 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!
}
}
为什么 UIGraphicsGetImageFromCurrentImageContext 返回 nil?和图片大小有关系吗?
(lldb) po self.size ▿ (3024.0, 4032.0) - 宽度:3024.0 - 高度:4032.0
【问题讨论】:
-
"为什么 UIGraphicsEndImageContext 返回 nil?是和图片大小有关吗?"好问题。你为什么不回答呢?试试小图片看看吧!
-
也可以调用
UIGraphicsGetCurrentContext,查看上下文本身是否为nil。 -
我正在调用 UIGraphicsGetCurrentContext 并且它返回 nil...
-
好吧,那就这样吧。
-
尺寸为 3 的 3024 x 4032(三倍分辨率设备)将具有超过 3600 万像素。这些数字是不是让你有点害怕?