我已閱讀有關該主題的多個主題,但我的問題仍然存在。 當我使用以下代碼調整圖像大小時:調整大小UIImage時發生內存泄漏
extension UIImage {
func thumbnailWithMaxSize(image:UIImage, maxSize: CGFloat) -> UIImage {
let width = image.size.width
let height = image.size.height
var sizeX: CGFloat = 0
var sizeY: CGFloat = 0
if width > height {
sizeX = maxSize
sizeY = maxSize * height/width
}
else {
sizeY = maxSize
sizeX = maxSize * width/height
}
UIGraphicsBeginImageContext(CGSize(width: sizeX, height: sizeY))
let rect = CGRect(x: 0.0, y: 0.0, width: sizeX, height: sizeY)
UIGraphicsBeginImageContext(rect.size)
draw(in: rect)
let thumbnail = UIGraphicsGetImageFromCurrentImageContext()!;
UIGraphicsEndImageContext()
return thumbnail
}
override func viewDidLoad() {
super.viewDidLoad()
let lionImage = UIImage(named: "lion.jpg")!
var thumb = UIImage()
autoreleasepool {
thumb = lionImage.thumbnailWithMaxSize(image: lionImage, maxSize: 2000)
}
myImageView.image = thumb
}
...內存未釋放。所以當我瀏覽多個ViewController時(例如使用PageViewController),我最終得到內存警告,最終應用程序崩潰。 我也嘗試通過UIImage(contentsOfFile:path)加載圖像,但沒有成功。 有什麼建議嗎?
就是這樣!謝謝。 – Thoms