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
Related
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.
In my app I have an array that looks like this:
var imgArray = [(Data, String)]()
It holds images and the images names and I append data by doing this:
if let firstImage = self.firstImage {
if let firstImageData = firstImage.compressImage() {
self.imgArray.append(firstImageData, self.randomImageName(length: 15))
}
}
Now I need to send all the image names inside that array using alamofire and I tried this:
for imgName in self.imgArray {
parameters["images[]"] = imgName.1
}
But my server responds by saying that images[] is empty, so I think that my ios code never sends an array named images[] containing the image names
Update
I changed the parameter to this:
parameters["images"] = [imgName.1]
This will upload one image, it seems like it overwrites the value each time so I need a way to grap all Strings from the imgArray array
Solution:
parameters["images"] = imgArray.map{$0.1}
This will add all of the image names from imgArray
I was trying to add image literals to array and display them as per the index.
Here is my code :
var images = [#imageLiteral(resourceName: "male-circle-128"),#imageLiteral(resourceName: "add_to_favourite-128"),#imageLiteral(resourceName: "28468-200"),#imageLiteral(resourceName: "progress_circular"),#imageLiteral(resourceName: "logout-1-128")]
and showing like this
cell!.imageView?.image = UIImage.init(cgImage: images[indexPath.row] as! CGImage)
got EXC_BAD_INSTRUCTION! what is the proper way to do this
let images:[UIImage] = [array of image literal goes here]
You can simply create array of image literals like:
var images = [#imageLiteral(resourceName: "image1"),#imageLiteral(resourceName: "image2"]
Why do you cast to CGImage , your literals are UIImage and you can use them without casting and even if you want CGImage use the initializer not casting.
let images = [literal images are here]
imageview.image = images[indexPath.row]
I try to create new array of UIImage objects:
var images = [UIImage](repeating: nil, count: 10)
for i in 0..<10
{
images[i] = ...
}
But I got this error (at first row):
Expression type [UIImage] is ambiguous without more context
You are trying to declare an array with UIImage objects and then instantiating with nil. Try
var images = [UIImage?](repeating: nil, count: 10)
and then handle images being nil when you access them.
If you already know how you are going to populate images, you can use the map function for arrays like so:
var images = (0..<10).map { (i) -> UIImage in
return UIImage() // however you are trying to get the UIImage
}
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)