Reputation: 2638
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
Upvotes: 3
Views: 4349
Reputation: 4935
You can simply create array of image literals like:
var images = [#imageLiteral(resourceName: "image1"),#imageLiteral(resourceName: "image2"]
Upvotes: 1
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
Reputation: 1391
let images:[UIImage] = [array of image literal goes here]
Upvotes: 2