iOS 加载图像时应用程序崩溃
iOS app crashes when loading an image
目标:
- 以编程方式将第一张图片(约 94kb)从 Assets.xcassets 文件夹(参见下面的代码)加载到主情节提要中的 ImageView - 工作完美
- 然后,当您将第二张图片(相同大小)加载到原始 UIImage 中时,它会导致 iOS 应用程序崩溃。
这是我的代码:
mainImageView.image = UIImage(named:"FirstImage.png") // load first image, no issues
然后,如果您以编程方式将第二个图像加载到同一个 UIImage 中,它会导致设备抛出内存不足警告并且 iOS 使应用程序崩溃:
mainImageView.image = UIImage(named:"SecondImage.png") // load second image
在阅读了关于 SO 和其他文章(见下文)的大量答案后,在将多个图像加载到动画数组中时管理内存的最佳方法是使用 contentsOfFile: imageName
而不是 UIImage(named:"FirstImage.png")
在此处查看文章:
http://www.alexcurylo.com/2009/01/13/imagenamed-is-evil/
其次,Apple 声明如下:
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. In iOS 9 and later, this
method is thread safe. 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.
https://developer.apple.com/documentation/uikit/uiimage/1624146-init
最后,在收到内存警告后,您还可以创建以下函数:
func applicationDidReceiveMemoryWarning(application: UIApplication) {
NSURLCache.sharedURLCache().removeAllCachedResponses()
}
希望这对其他人有帮助:)
目标:
- 以编程方式将第一张图片(约 94kb)从 Assets.xcassets 文件夹(参见下面的代码)加载到主情节提要中的 ImageView - 工作完美
- 然后,当您将第二张图片(相同大小)加载到原始 UIImage 中时,它会导致 iOS 应用程序崩溃。
这是我的代码:
mainImageView.image = UIImage(named:"FirstImage.png") // load first image, no issues
然后,如果您以编程方式将第二个图像加载到同一个 UIImage 中,它会导致设备抛出内存不足警告并且 iOS 使应用程序崩溃:
mainImageView.image = UIImage(named:"SecondImage.png") // load second image
在阅读了关于 SO 和其他文章(见下文)的大量答案后,在将多个图像加载到动画数组中时管理内存的最佳方法是使用 contentsOfFile: imageName
而不是 UIImage(named:"FirstImage.png")
在此处查看文章:
http://www.alexcurylo.com/2009/01/13/imagenamed-is-evil/
其次,Apple 声明如下:
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. In iOS 9 and later, this method is thread safe. 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.
https://developer.apple.com/documentation/uikit/uiimage/1624146-init
最后,在收到内存警告后,您还可以创建以下函数:
func applicationDidReceiveMemoryWarning(application: UIApplication) {
NSURLCache.sharedURLCache().removeAllCachedResponses()
}
希望这对其他人有帮助:)