我的应用程序在 iPad Mini 上显示内存警告后崩溃。经过大量研究,我发现问题是由于使用 UIImage(named:)
进行内存分配造成的,例如:
SkipTutorial.image = UIImage(named: "skipTutorial.png")
我应该使用 contentsOfFile
方法,这样图像就不会被缓存。所以我用了:
if let imgPath = NSBundle.mainBundle().pathForResource("skipTutorial", ofType: "png") {
SkipTutorial.image = UIImage(contentsOfFile:imgPath)
}
但是这不会获取图像。这些图像位于 Xcode 中的 Images.xcassets 中。基本上我的应用程序使用了大量不需要缓存的图像。我在这里发现了类似的问题:
How can I stop my swift app from crashing
甚至他的询问在如何使用 contentsOfFile
方法的注释中也没有得到解答。如果有人在这里帮助我,我将非常感激。
最佳答案
据我所知,您不能使用xcassets
。
只需将图像包含在 Xcode 的文件夹中,确保将它们复制到 bundle 中,然后使用
访问它们let path = NSBundle.mainBundle().pathForResource(imageName, ofType: "png")
let image = UIImage(contentsOfFile: path)
关于ios - 使用 contentOfFile 而不是名为的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29572225/