加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xcode – 使用contentsOfFile的UIImage

发布时间:2020-12-15 01:49:13 所属栏目:百科 来源:网络整理
导读:我在用 self.imageView.image = UIImage(名称:“foo.png”) 在UIIMageView中选择和加载图像.我在images.xcassets下的应用程序中有图像.问题是这个特定的init缓存图像以便按照official Apple documentation重用: If you have an image file that will only
我在用

self.imageView.image = UIImage(名称:“foo.png”)

在UIIMageView中选择和加载图像.我在images.xcassets下的应用程序中有图像.问题是这个特定的init缓存图像以便按照official Apple documentation重用:

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(contentsOfFile:“文件路径”).在这里,我无法以编程方式获取我在images.xcassets下存储的图像的路径.

我试过用过:

NSBundle.mainBundle().resourcePath
和NSBundle.mainBundle().pathForResource(“foo”,ofType:“png”)

没有运气.对于第一个我得到了resourcePath,但是在通过终端访问它时我没有看到它下面的任何图像资产,而第二个我在使用它时得到nil.有一个简单的方法来做到这一点?

还看了几个SO问题(如this,this和this)没有运气.我是否必须将我的图像放在其他地方才能使用pathForResource()?什么是正确的方法?

很难想象之前没有人遇到过这种情况:)!

解决方法

如果需要使用pathForResource()来避免图像缓存,则无法使用images.xcassets.在这种情况下,您需要在XCode中创建组,并在那里添加图像(确保将该图像复制到Copy Bundle Resources).之后只写:

var bundlePath = NSBundle.mainBundle().pathForResource("imageName",ofType: "jpg") 
var image = UIImage(contentsOfFile: bundlePath!)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读