iOS UI测试:获取图像文件名(Swift) [英] iOS UI Test: Get image filename (Swift)

查看:628
本文介绍了iOS UI测试:获取图像文件名(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是获取在 UIImageView 中使用的 UIImage 的图像文件名。

My question is about getting the filename of image of the UIImage that is used in a UIImageView.

这是我的示例代码:

// Using image from UIImageObject
imageView1.image = myUIImage
// Using image from XAssets
imageView2.image = UIImage(named: "myImageName")

在UI测试中,如何获取图像文件的名称?
预期的resoult将是:

In UI Tests, how can I get the name of the image file? The expected resoult would be:

"myUImageObject" // For imageView1 
"myImageName" // For imageView2

有没有办法获得这个值?

Is there any way to get this value?

谢谢大家!

推荐答案

不幸的是你无法做到这一点,但有一个简单的解决方法,尝试像这个:

Unfortunately you can't do that, however there's an easy workaround, try something like this:

let myImageView = UIImageView()
myImageView.image = UIImage(named: "anyImage")
myImageView.restorationIdentifier = "anyImage" // Same name as image's name!

// Later, in UI Tests:
print(myImageView.restorationIdentifier!) // Prints "anyImage"

在此解决方案中,您使用恢复标识符来保存图像的名称,因此您可以在以后的任何地方使用它。如果更新映像,则还必须更新还原标识符,如下所示:

Basically in this solution you're using the restoration identifier to hold the image's name, so you can use it later anywhere. If you update the image, you must also update the restoration identifier, like this:

myImageView.restorationIdentifier = "newImageName"

我希望能帮到你,祝你好运!

I hope that helps you, good luck!

这篇关于iOS UI测试:获取图像文件名(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆