如何从 RAM 内存中清除图像
How to clear an image from RAM memory
每次我调用 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:)
。
同意@Rob 的回答,
只是对冗余代码的一个改进,不需要使用switch-case
而是可以使用,
@IBAction func changeImage(_ sender: Any) {
index += 1
img.image = UIImage(named: "i\(index)")
}
每次我调用 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:)
。
同意@Rob 的回答,
只是对冗余代码的一个改进,不需要使用switch-case
而是可以使用,
@IBAction func changeImage(_ sender: Any) {
index += 1
img.image = UIImage(named: "i\(index)")
}