iOS UI Test: Get image filename (Swift) - ios

My question is about getting the filename of image of the UIImage that is used in a UIImageView.
Here is my sample code:
// Using image from UIImageObject
imageView1.image = myUIImage
// Using image from XAssets
imageView2.image = UIImage(named: "myImageName")
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?
Thanks everyone!

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!

Swift 4
Declare Outlet
#IBOutlet weak var m_SetName_Img: UIImageView! //Image is already set image from Assets File
Declare Function
extension UIImageView {
func getImageName() -> String {
if let image = self.image, let imageName = image.accessibilityIdentifier {
return imageName
} else {
return "nil"
}
}
}
Use
print(self.m_SetName_Img.getImageName())

You can use:
imageView2.image?.imageAsset?.value(forKey: "assetName")
assetName is private API, so officially out of bounds for the App Store, but for testing it should be just fine.

Related

Create a UIImageView subclass with a String tag?

I would like to get a functionality in which when you set your image with a file, the UIImageView subclass will automatically save this in a var stringTag:String!
If I simply create the subclass with this var, I will have to manually set it's tag name when I set the image :
imageView.image ="abc.png"
imageView.tagString="abc.png"
Is there a simple way to create this subclass so when I set the image, it will save it's file name as a tag var automatically ?
Unfortunately it's not possible. Because UIImage doesn't store any information about file path or name.
One way to achieve something similar to what you want is to do something like this:
class TaggableImageView:UIImageView
{
var tagString:String?
var imageName:String?
{
didSet
{
self.image = UIImage(named:imageName)
self.tagString = imageName
}
}
}

Mapbox : MGLSymbolStyleLayer add my custom "iconImage"

How can i add custom image for MGLSymbolStyleLayer. Below is my code,
let symbolGraphicsLayer = MGLSymbolStyleLayer(identifier: identifier, source: source)
symbolGraphicsLayer.sourceLayerIdentifier = identifier
symbolGraphicsLayer.iconImageName = MGLStyleConstantValue<NSString>(rawValue: "assets/myImage")
symbolGraphicsLayer.iconScale = MGLStyleValue(rawValue: 1)
symbolGraphicsLayer.isVisible = true
self.mapView.style?.addLayer(symbolGraphicsLayer)
Thanks.
Is your problem that the image doesn't appear?
You first need to add the image to the style layer, and then you can use it.
So before that code, you can do:
if let image = UIImage(named: "myImage") {
mapView.style?.setImage(image, forName: "myImage")
}
and you can use it afterwards like you said. Just use the name you passed to the setImage method.
I hope this helps other people since the documentation is quite poor for this

UIButton Background image is not set from Singleton Dictionary

again.
I've just finished a singleton example of a Dictionary with the following code
at the file Recipes.swift
static let sharedInstance = Recipes()
var imagesOfChef = [Int : chefImages]()
struct chefImages {
var objidChef: String!
var imageChef: UIImage!
}
And when a process is finished i have this code
let singleton = Recipes.sharedInstance
singleton.imagesOfChef = self.imagesOfChef
So the singleton has the same dictionary as the imagesOfChef has.
On the next view i want to access all the data of the Dictionary and set a background image of the button with one of the images that the Dictionary has.
So in the cellForItemAtIndexPath I have the following code
let test = userPointer.objectId
let otherReferenceToTheSameSingleton = Recipes.sharedInstance
for i in 0...otherReferenceToTheSameSingleton.imagesOfChef.count - 1 {
print(otherReferenceToTheSameSingleton.imagesOfChef[i]!.objidChef) //it prints the object id's of the Chef normally
if otherReferenceToTheSameSingleton.imagesOfChef[i]!.objidChef == test! {
print("done")
print(otherReferenceToTheSameSingleton.imagesOfChef[i]!.imageChef)
cell.avatarOutlet.setImage(otherReferenceToTheSameSingleton.imagesOfChef[i]!.imageChef, forState: .Normal) //but it doesnt set the Image as a background
}
}
So when the object id of the Chef matches the object id that is inside the Dictionary i want to set as background the image that is in the same row.
But it doesn't!
Although when the if statement is correct if you see i try to print the image and i get
<UIImage: 0x1582c9b50>, {60, 60}
How is it possible to set this image as background??!?!
Thanks a lot!
Set the Button type custom if it is created programmatically
let button = UIButton(type: UIButtonType.Custom) as UIButton
If it is from Stroyboard change the type there.

Want to use string to UIImage(named: String).center

I created the String array called Pillar and there are 26 Strings.
I want to use that Strings in Array as a UIImage's name and set that UIImage a CGPoint.
But there is an error that UIImage does not have the member center.
The error is:
Value of type 'UIImage?' has no member 'center'
The code is
var Pillar:[String]=["Pillar1","Pillar2","Pillar3",...,"Pillar26"]
self.Pillar1.center = CGPointMake(175.0, 436.0)
self.Pillar2.center = CGPointMake(214.0, 407.0)
for(let i=3; i<=26; i++) {
//here the error I occur
UIImage(named: Pillar[i]).center = CGPointMake(UIImage(named: Pillar[i-1]).center.x-5,UIImage(named: Pillar[i-1].center.y+4)
}
I just want to make iteration to emphasise the code by using the String array.
If not I have to write 26 lines of code. If anyone can help me please help.
Try this:
let images = ["Pillar1","Pillar2","Pillar3",...,"Pillar26"].map { UIImage(named: $0) }
And note that UIImageView has center property, NOT UIImage

How can I set the value of this variable?

I want to change the value of a UIImage view.
This is what I've got so far.
import UIKit
import Foundation
struct imageViews {
var Slope1ImageView: UIImage?
var slopeStatus = dataValues()
func setSlope1Image(slopeStatus1: Int) -> UIImage {
var image: String
switch slopeStatus1 {
case 0:
image = "NotWorkingStatus"
default:
image = "WorkingStatus"
}
var statusImage = UIImage(named: image)
return statusImage
}
}
This my setup.
I have a file which gets an object from Parse.
This will either be 0 or 1.
I then assign the 0 or the 1 to a variable in a struct.
In my code above I have created a instance of this struct.
I have then created a function which will take in a variable from my struct and check if it has the value of zero, it then sets the variable image to the respective image. If it doesn't it sets it to a different image.
How do I set the variable of Slope1ImageViews to the image selected by the function in this way.
Slope1ImageView = setSlope1Image(slopeStatus.SlopeStatus1)
each time I try I get an error along the lines of
cannot assign to "Slope1ImageView" in "self"
I'm at my wits end!
Any help would be greatly appreciated.
I think your problem is with Slope1ImageView. If the first letter is uppercased, Swift assumes you're referring to a class. Furthermore, you need to assign the local to either a variable or a constant. Structs should also have the first letter uppercased. Assuming everything else is set up correctly, you probably just need to do something like this.
struct ImageViews {
// existing code...
}
var instanceOfStruct = ImageViews()
var slope1imageView: UIImage = instanceOfStruct.setSlope1Image(slopeStatus.SlopeStatus1)
There's a lot going on here, I'll touch on some.
Declaration
You shouldn't be using a struct. Images are rather large objects in general and you don't want them to be copied across your app. They should remain reference types and be in a class. Structs/Classes should also be capitalized to adhere to common practice, while properties should be lower case. You also have your name as a plural which implies that it is an array, it shouldn't be this way. Also, don't name something imageView unless its an imageView. On that same line, don't name something image if it's a string. Also, don't prefix a function with set unless it sets something. Your function returns something, so a get prefix is probably better. I feel like you want to set the image within self, so let's do that. So first step is to change this:
struct imageViews {
var Slope1ImageView: UIImage?
var slopeStatus = dataValues()
func setSlope1Image(slopeStatus1: Int) -> UIImage {
var image: String
switch slopeStatus1 {
case 0:
image = "NotWorkingStatus"
default:
image = "WorkingStatus"
}
var statusImage = UIImage(named: image)
return statusImage
}
}
To this:
class SlopeStatus {
var slope1Image: UIImage?
var slopeStatus = dataValues()
func setSlope1Image(slopeStatus1: Int) {
var imageString: String
switch slopeStatus1 {
case 0:
image = "NotWorkingStatus"
default:
image = "WorkingStatus"
}
slope1Image = UIImage(named: image)
}
}
Assignment
You're trying to assign a UIImage to a Class with this:
Slope1ImageView = setSlope1Image(slopeStatus.SlopeStatus1)
With the changes above, it should probably be
var slopeStatusImageObject = SlopeStatus()
slopeStatusImageObject.setSlope1Image(slopeStatus.SlopeStatus1)

Resources