Swift 5 or up - Image view on pickerview - ios

My images from "2.jpg" to "7.jpg" shows but the first image("1.jpg") on my simulator. what do I need to change?
var imageFileName = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg"]
for i in 0 ..< MAX_ARRAY_NUM {
let image = UIImage(named: imageFileName[i])
imageArray.append(image)
lblImageFileName.text = imageFileName[0]
imageView.image = imageArray[0]
enter image description here
enter image description here

I think you should change below two lines
lblImageFileName.text = imageFileName[0]
imageView.image = imageArray[0]
to,
lblImageFileName.text = imageFileName[i]
imageView.image = imageArray[i]
as you used variable i to represent index

Related

UIImage not changing image because of EXC_BAD_ACCESS crash, why?

I have this conditional that results in a UIImageView being changed depending one what side a coined "flipped" (Int.rand(0...1). I am not sure why it is crashing at this particular spot, as before the program gets to this point it could change the exact same UIImageView with another image asset. The image asset I want to change it to does exist in the asset folder. It's just confusing why it is crashing at this particular instance.
func changeField(coin: Int){
lowerField.isUserInteractionEnabled = false
upperField.isUserInteractionEnabled = false
firstP1.isHidden = true
firstP2.isHidden = true
if coin == 0{
lowerField.image = UIImage(named: "Red_Attacker")
upperField.image = UIImage(named: "Blue_Defender")
if userSettings.float(forKey: "Timer_Slider") == 0{
gameP1()
}else{
startTimer(slider_Value: userSettings.float(forKey: "Timer_Slider"), coin: coin)
}
}else{
lowerField.image = UIImage(named: "Blue_Defender")
upperField.image = UIImage(named: "Red_Attacker")
if userSettings.float(forKey: "Timer_Slider") == 0{
gameP2()
}else{
startTimer(slider_Value: userSettings.float(forKey: "Timer_Slider"), coin: coin)
}
}
}
this shows where it specifically throws the exception
Anyone have any idea why it is crashing at this spot? Would appreciate the help!
first make sure that you have an image with this name
second thing try to put it in dispatchQueue:
DispatchQueue.main.async {
lowerField.image = UIImage(named: "Red_Attacker")
upperField.image = UIImage(named: "Blue_Defender")
}
DispatchQueue.main.async {
lowerField.image = UIImage(named: "Blue_Defender")
upperField.image = UIImage(named: "Red_Attacker")
}

How to display an image if variable equals a special number using swift 3?

I have three pictures (lvl1.png, lvl2.png, lvl3.png) and an variable (let level = 1). What should I do to display an image named 'lvl2' if level = 2, and when level = 3 I need to show the last image (lvl3.png)?
you can use:
let image = UIImage(named: "lvl\(level).png")
or
let image: UIImage!
switch level {
case 1:
image = UIImage(named: "lvl1.png")
case 2:
image = UIImage(named: "lvl2.png")
case 3:
image = UIImage(named: "lvl3.png")
default:
image = UIImage()
}
avatar.image = image
you can try
imgName = "lvl" + String(lavel)
imageView.image = UIImage(named: imgName)
Another alternative is to create a method that returns an image, like so:
func imageFor(level: Int) -> UIImage? {
let image = UIImage(named: "lvl\(currentLevel)")
return image
}
Usage:
var currentLevel = 1
let image = imageFor(level: currentLevel)
You can try
var level = 1 {
didSet {
self.imageview.image = UIImage(named: "lvl\(level)")
}
}
when you change the level the imageView will change automatically

Animate icons change

I am working on my first application, its a mathematical riddles app. The player can get a hint that will reveal one of the variables - it's basically replacing one image with another. Sometimes I am replacing more than one image so I am using a loop that replace all of them. I want the old image to fade and be replaced with the new image, the answer. Also I would like them to fade one after the other, meaning that there will be a small delay between one image replacement animation to the next.
func changeHintIcons () {
var labelsArr = [[firstEquationFirstElemnt,firstEquationSecondElemnt,firstEquationThirdElemnt],[secondEquationFirstElemnt,secondEquationSecondElemnt,secondEquationThirdElemnt],[thirdEquationFirstElemnt,thirdEquationSecondElemnt,thirdEquationthirdElemnt],[fourthEquationFirstElemnt,fourthEquationSecondElemnt,fourthEquationThirdElemnt], [fifthEquationFirstElemnt,fifthEquationSecondElemnt,fifthEquationThirdElemnt]]
let col:Int = Int(arc4random_uniform(UInt32(gameDifficulty.stages[gameLevel].umberOfVariables)))
let row:Int = Int(arc4random_uniform(UInt32(2))) * 2
let var_to_show = current_equations[col][row]
let image_name = "answer.num.\(var_to_show)"
for i in 0..<current_equations.count {
for j in 0..<current_equations[i].count {
if (current_equations[i][j] == var_to_show) {
var image_index = j
if (j > 0) {
image_index = Int(j/2) //Converting index
}
labelsArr[i][image_index]!.image = UIImage(named: image_name)! //Replacing the image
}
}
}
}
One last thing, what if I want to use in an animation instead of letting the image simply fade out? What are my options and how can I implement them?
Ok I found the answer. Basically swift allows you to create your animation by projecting a set of images one after the other. Follow these steps:
1. Copy animation images to assets folder
2. create an array of UIImages
3. Do the same things as I did in the animate function
Main code -
var animationArray = createImageArray(total: 14, imagePrefix: "hint.animation")
animationArray.append(UIImage(named: imageHintAnswer)!)
animate(imageView: labelsArr[i][image_index]!, images: animationArray)
Functions -
func createImageArray(total: Int, imagePrefix: String) -> [UIImage] {
var imageArray:[UIImage] = []
for imageCount in 1..<total {
let imageName = "\(imagePrefix).\(imageCount)"
let image = UIImage(named: imageName)!
imageArray.append(image)
}
return imageArray
}
func animate(imageView: UIImageView, images: [UIImage]) {
imageView.animationImages = images
imageView.animationDuration = 0.7
imageView.animationRepeatCount = 1
imageView.startAnimating()
}

UIImageView animation is not working [duplicate]

This question already has answers here:
Swift - How to animate Images?
(7 answers)
Closed 6 years ago.
I am working on the UIImageView transition in swift. I an storing six images in an array and giving that array to “imageview.animationImages
” and I am trying to making it to work as ‘Gif animation’ the code I wrote is as follows :
logoImages = NSMutableArray(array: ["backGround4.jpeg","backGround3.jpeg","backGround.jpeg","16.jpeg","23.jpeg","backGround1.jpeg"])
imageview.animationImages = NSArray(array: ["backGround4.jpeg","backGround3.jpeg","backGround.jpeg","16.jpeg","23.jpeg","backGround1.jpeg"]) as? [UIImage]
imageview.animationDuration = 1.5
imageview.animationRepeatCount = 1
imageview.startAnimating()
It is not working.Can anyone please tell me what is the mistake I am doing here?
Thanks In Advance
First correct your image name like backGround1.jpeg, backGround2.jpeg etc..
After that create one image array which will hold all your images:
var imageArray = [UIImage]()
Then add all images in image array:
for var i = 1; i < totalImageCount; i++ {
let image = UIImage(named: "backGround4\(i).png")
imageArray.append(image!)
}
After that you can create animation this way:
imageview.animationImages = imageArray
imageview.animationDuration = 1.5
imageview.startAnimating()
Please convert string to UIImage like this:
imageview.animationImages = NSArray(array: ["backGround4.jpeg","backGround3.jpeg","backGround.jpeg","16.jpeg","23.jpeg","backGround1.jpeg"].map({ (string) -> UIImage in
UIImage(named: string)! })) as? [UIImage]
Please try this
var images: [UIImage] = []
for i in 1...2 {
images.append(UIImage(named: "c\(i)")!)
}
myImageView.animationImages = images
myImageView.animationDuration = 1.0
myImageView.startAnimating()

Randomly select a UIImage

I need to pick one of these dots. I add them all before viewDidLoad, I need it to pick a random one. My current code returns the error cannot assign to 'openingScreenDynamicDot in 'self'. How is this fixed?
CODE:
let openingScreenDynamicDot = UIImage()
let dotOne = UIImage(named: "dot1.png")
let dotTwo = UIImage(named: "dot2.png")
let dotThree = UIImage(named: "dot3.png")
let dotFour = UIImage(named: "dot4.png")
let dotFive = UIImage(named: "dot5.png")
let dotSix = UIImage(named: "dot6.png")
let dotSeven = UIImage(named: "dot7.png")
let dotEight = UIImage(named: "dot8.png")
let dotNine = UIImage(named: "dot9.png")
let dotTen = UIImage(named: "dot10.png")
let dotEleven = UIImage(named: "dot11.png")
let dotTwelve = UIImage(named: "dot12.png")
let dotThirteen = UIImage(named: "dot13.png")
var imageNumber = arc4random()%13
override func viewDidLoad() {
let theRandomImages = [dotOne, dotTwo, dotThree, dotFour, dotFive, dotSix, dotSeven, dotEight, dotNine, dotTen, dotEleven, dotTwelve, dotThirteen]
openingScreenDynamicDot = theRandomImages.objectAtIndex(imageNumber)
}
You declare constants with the let keyword and variables with the var keyword.
So change let openingScreenDynamicDot to var openingScreenDynamicDot
also, a swift native array does not have a objectAtIndex method so..
change
openingScreenDynamicDot = theRandomImages.objectAtIndex(imageNumber)
to
openingScreenDynamicDot = theRandomImages[imageNumber]
You can try this code to generate one of your random "dots".
override func viewDidLoad(){
let openingScreenDynamicDot.image = UIImage(named: "dot\(arc4random_uniform(13) + 1).png")
}
This is called "String Interpolation". For more info, Click here.
I hope this can help you if you are still having problems.

Resources