我有这个 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
看起来 UIGraphicsBeginImageContextWithOptions 已实现,以便它尝试为图像缓冲区分配连续的内存区域。该区域非常大,可以达到 50MB。发生的情况是“malloc”调用返回空值,并且对 UIGraphicsBeginImageContextWithOptions 的调用无声无息地失败。malloc失败的原因可能从内存不足到内存碎片不同,这就是这个问题的重现不一致的原因。
| 归档时间: |
|
| 查看次数: |
3684 次 |
| 最近记录: |