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]
Related
I am trying to create a slideshow
I follow this zvonicek/ImageSlideshow library.
But I don't know how to pass array of images to it
I have the following code:
let imageSource = [SDWebImageSource(urlString:self.myArray[i])!, ImageSource(image: UIImage(named: "Beach")!)]
slideshow.setImageInputs(imageSource as! [InputSource])
self.myArray[i] contains that kind of value
and I need to pass that array to imageSource.
Essentially, what you are trying to do is to transform each element of a [String] to a SDWebImageSource, so that it forms a [SDWebImageSource]. Right? This is precisely the job of the map method.
let imageSource = self.myArray.map { SDWebImageSource(urlString: $0) }
Or more simply:
let imageSource = self.myArray.map(SDWebImageSource.init))
As mentioned in the loading images section in the description of the library, if you have url for the images. You can use the AlamofireSource or KingfisherSource to create the image Source array.
ImageSource(image: UIImage(named: "myImage"))!,
ImageSource(image: UIImage(named: "myImage2"))!,
AlamofireSource(urlString: "https://images.unsplash.com/photo-1432679963831-2dab49187847?w=1080"),
KingfisherSource(urlString: "https://images.unsplash.com/photo-1432679963831-2dab49187847?w=1080"),
ParseSource(file: PFFile(name:"image.jpg", data:data))
])
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.
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 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
I'm trying save some images to an array, and their name to another array as string with below code;
self.countryNamesArray.append(object["CountryName"] as! String)
let image = UIImage(data:imageData!)
self.countryImages.append(image!)
But it doesn't retrieve images with same order I saved while retrieve CountryNamesArray with correct order.
rowString = countryNamesArray[row]
myImageView.image = self.countryImages[row]
Should I use custom class to save images with string or are there any way to fix?