Vinu David Jose
Vinu David Jose

Reputation: 2638

how to add image literal to an array and show them?

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

enter image description here

Upvotes: 3

Views: 4349

Answers (3)

Abdul Karim Khan
Abdul Karim Khan

Reputation: 4935

You can simply create array of image literals like:

var images = [#imageLiteral(resourceName: "image1"),#imageLiteral(resourceName: "image2"]

Upvotes: 1

user7649191
user7649191

Reputation:

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]

Upvotes: 0

Ajay Singh Mehra
Ajay Singh Mehra

Reputation: 1391

let images:[UIImage] = [array of image literal goes here]

Upvotes: 2

Related Questions