ios - 使用 contentOfFile 而不是名为的图像

标签 ios swift

我的应用程序在 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/

相关文章:

swift - 为什么#selector() 不能快速工作

ios - 使用 Helvetica Neue Light - Html5 IOS 应用程序

ios - SNSPublishRequest 如何发送 aps 数组并且看不到 APNS 的 "default"消息

ios - swift ,NSOperationQueue.mainQueue() : update data in operation during operation is running?

ios - ContentView 的自定义渲染器 (iOS)

ios - 在 swift 2.2 中使用 2d 字典中的循环添加数据

ios - 如何使用自动布局将微调器居中?

ios - 如何在表格中制作@IBAction?

ios - 标签栏 Controller : Add/Remove from Array

ios - 如何一起使用 NSFetchedResultsController 和 UISearchController?