使用contentsOfFile而不是图像命名

Sam*_*ain 2 ios swift

在iPad Mini上显示内存警告后,我的应用程序应用程序崩溃.经过大量研究后,我发现问题是由于内存分配所致,UIImage( named:)例如:

SkipTutorial.image = UIImage(named: "skipTutorial.png")
Run Code Online (Sandbox Code Playgroud)

我应该使用contentsOfFile方法,以便不缓存图像.所以我用过:

if let imgPath = NSBundle.mainBundle().pathForResource("skipTutorial", ofType: "png") {
     SkipTutorial.image = UIImage(contentsOfFile:imgPath)
}
Run Code Online (Sandbox Code Playgroud)

但是,这不会获取图像.图像位于Xcode 中的Images.xcassets中.基本上我的应用程序使用了许多不需要缓存的图像.我在这里发现了类似的问题:

如何阻止我的快速应用程序崩溃

甚至他的查询在如何使用contentsOfFile方法的评论中都没有得到答复.如果有人帮助我,我真的很感激.

Mun*_*ndi 6

xcassets据我所知,你无法使用.

只需将您的图像包含在Xcode的文件夹中,确保将它们复制到捆绑包中,然后使用它们访问它们

let path = NSBundle.mainBundle().pathForResource(imageName, ofType: "png")
let image = UIImage(contentsOfFile: path)
Run Code Online (Sandbox Code Playgroud)