gpt4 book ai didi

ios - 如何从 RAM 内存中清除图像

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

每次我调用 changeImage 函数时,img 图像都会改变。问题是每次更改照片时,应用程序使用的 RAM 内存都会增加,即使以前的照片不再显示在屏幕上也是如此。

enter code here import UIKit
class ViewController: UIViewController {

var takenImage = UIImage()

@IBOutlet var img: UIImageView!
@IBOutlet var buton: UIButton!

var index = 0

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func changeImage(_ sender: Any) {
index+=1

switch(index) {
case 1: img.image = UIImage(named: "i1")
case 2: img.image = UIImage(named: "i2")
case 3: img.image = UIImage(named: "i3")
case 4: img.image = UIImage(named: "i4")
case 5: img.image = UIImage(named: "i5")
case 6: img.image = UIImage(named: "i6")

default: print("default");
}
}
}

我希望从 RAM 内存中删除旧照片

最佳答案

UImage(named:) 缓存图像(将它们保存在内存中以在您再次使用它们时提高性能)。除非有内存压力,否则它们不会从内存中删除。

作为the documentation说:

Discussion

This method looks in the system caches for an image object with the specified name and returns the variant of that image that is best suited for the main screen. If a matching image object is not already in the cache, this method locates and loads the image data from disk or from an available asset catalog, and then returns the resulting object.

The system may purge cached image data at any time to free up memory. Purging occurs only for images that are in the cache but are not currently being used.

...

Special Considerations

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

最重要的是,如果这是您将在应用中重复使用的图像,您可以选择继续使用 UIImage(named:),确信如果存在内存压力,图片将被释放。但如果这不是您将重复使用的图像,请考虑改用 UIImage(contentsOfFile:)

关于ios - 如何从 RAM 内存中清除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53962156/

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