gpt4 book ai didi

ios - UIGraphicsGetImageFromCurrentImageContext() 返回 nil

转载 作者:搜寻专家 更新时间:2023-10-31 22:56:18 29 4
gpt4 key购买 nike

我有这个 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

enter image description here

最佳答案

看起来 UIGraphicsBeginImageContextWithOptions 的实现是为了尝试为图像缓冲区分配一个连续的内存区域。该区域非常大,可以达到 50MB。发生的情况是“malloc”调用返回 null,并且对 UIGraphicsBeginImageContextWithOptions 的调用静默失败。 malloc失败的原因可能有内存不足,也有可能是内存碎片,这也是该问题重现不一致的原因。

关于ios - UIGraphicsGetImageFromCurrentImageContext() 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46100293/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com