ios swift,如何从 UIImageView 获取当前显示的图像文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28515845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
swift, How to get currently displayed image file name from UIImageView
提问by semirturgay
I want to get the image file name which is currently displayed at UIImageView. I tried to get it as follow:
我想获取当前显示在 UIImageView 的图像文件名。我试图得到它如下:
let currentImage = alien.image // !alien is my image view
println(currentImage?.description)
but it prints:
但它打印:
Optional("<UIImage: 0x7fa61944c3d0>")
回答by Fogmeister
You can't do this. Neither in swift nor objective-c.
你不能这样做。无论是在 swift 还是 Objective-c 中。
The thing to do is to store the data you want to retrieve. That is... store the name somewhere and use that to load the image. Not the other way around.
要做的事情是存储要检索的数据。那就是...将名称存储在某处并使用它来加载图像。不是反过来。
So create a property something like imageName
and then use that to load the image.
因此,创建一个类似的属性imageName
,然后使用它来加载图像。
回答by Christopher Wade Cantley
As a work around, for images that I need to reference at a later time, I use the restoration ID to store the image name.
作为一种变通方法,对于我以后需要参考的图像,我使用恢复 ID 来存储图像名称。
I used restoration ID in this way so that I could connect multiple buttons to the same @IBAction and identify them based on the image name stored in the restoration ID and run logic about what I want to display or hide.
我以这种方式使用了恢复 ID,以便我可以将多个按钮连接到同一个 @IBAction 并根据存储在恢复 ID 中的图像名称识别它们,并运行有关我想要显示或隐藏的内容的逻辑。
There might be better ways but this worked in a pinch.
可能有更好的方法,但这在紧要关头。
I put the image name in as the restoration ID.
我把图像名称作为恢复 ID。
Here is where I designate the file for the image..
这是我指定图像文件的地方..
And I just copy that and put it in as the restoration ID.
我只是复制它并将其作为恢复 ID 放入。
(note: that is not what this was intended to be used for as it is really meant for customizing state reference but if that is not relevant to the purpose of your view, then it should work fine.)
(注意:这不是它的用途,因为它实际上是用于自定义状态引用,但如果这与您的视图的目的无关,那么它应该可以正常工作。)
Referenced in code when the button is selected.
选择按钮时在代码中引用。
//Connected to several onboarding buttons.
@IBAction func onBoardingButton(sender: UIButton) {
println(sender.restorationIdentifier)
}
RID printed out.
RID 打印出来。
You can also tag your images and keep the reference to those images via the tag.
您还可以标记图像并通过标记保留对这些图像的引用。
And the reference is just as easy.
参考也很简单。
@IBAction func onBoardingButton(sender: UIButton) {
println(sender.restorationIdentifier!)
println(sender.tag)
}
While it doesn't seem like we can discern what file was used to fill the imageview (that I know of and based on a little looking around myself) attaching hard references to a view (image, button, etc..) allows me to make the connection code side and figure out which image (or in my case button) is being used.
虽然我们似乎无法辨别用于填充图像视图的文件(我知道并且基于我自己的一点环顾)将硬引用附加到视图(图像、按钮等)允许我制作连接代码并找出正在使用的图像(或在我的情况下是按钮)。