Renato Pimpão
Renato Pimpão

Reputation: 271

"Array index out of range" when choosing photos in Swift

I cant figure out what's the problem here.

I have 6 images and when i click on them it popsup a image picker. If i choose a photo with a sequence, like 1,2,3,4,5,6... it works. But when i choose the first photo that's not the first one (array[0]), i get an error:

`$"Array index out of range"

Here is my code:

class SecondViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var imagemEscolhida = -1

@IBOutlet var firstImageView: UIImageView!
@IBOutlet var secondImageView: UIImageView!
@IBOutlet var thirdImageView: UIImageView!
@IBOutlet var fourthImageView: UIImageView!
@IBOutlet var fifthImageView: UIImageView!
@IBOutlet var sixthImageView: UIImageView!


@IBAction func firstImageButton(sender: AnyObject) {

    imagemEscolhida = 1
    abreBibliotecaFotos(imagemEscolhida)

}

@IBAction func secondImageButton(sender: AnyObject) {
    imagemEscolhida = 2
    abreBibliotecaFotos(imagemEscolhida)

}

@IBAction func thirdImageButton(sender: AnyObject) {
    imagemEscolhida = 3
    abreBibliotecaFotos(imagemEscolhida)
}

@IBAction func fourthImageButton(sender: AnyObject) {
    imagemEscolhida = 4
    abreBibliotecaFotos(imagemEscolhida)
}

@IBAction func fifthImageButton(sender: AnyObject) {
    imagemEscolhida = 5
    abreBibliotecaFotos(imagemEscolhida)
}

@IBAction func sixthImageButton(sender: AnyObject) {
    imagemEscolhida = 6
    abreBibliotecaFotos(imagemEscolhida)
}

func abreBibliotecaFotos (img: Int )
{
    imagePicker.allowsEditing = false
    imagePicker.sourceType = .PhotoLibrary

    presentViewController(imagePicker, animated: true, completion: nil)
}



let imagePicker = UIImagePickerController()

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        firstImageView.contentMode = .ScaleAspectFit

        restaurante.imagem.insert(pickedImage, atIndex: imagemEscolhida-1)

        switch imagemEscolhida
        {
        case 1:
            firstImageView.image = restaurante.imagem[0]
        case 2:
            secondImageView.image = restaurante.imagem[1]
        case 3:
            thirdImageView.image = restaurante.imagem[2]
        case 4:
            fourthImageView.image = restaurante.imagem[3]
        case 5:
            fifthImageView.image = restaurante.imagem[4]
        case 6:
            sixthImageView.image = restaurante.imagem[5]
        default:
            println("Something else")
        }



        picker.dismissViewControllerAnimated(true, completion: nil)

    }

    dismissViewControllerAnimated(true, completion: nil)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissViewControllerAnimated(true, completion: nil)
}


override func viewDidLoad() {
    super.viewDidLoad()

    imagePicker.delegate = self
}


}

Upvotes: 0

Views: 247

Answers (2)

Bierbarbar
Bierbarbar

Reputation: 1479

You should initialize the array before you use it in the correct size. I would like to refer to my answer to a really similar question. So initialize it from 0 to 5 at definition then your code should work.

Upvotes: 2

Adam
Adam

Reputation: 26917

It looks like the restaurante.imagem array is empty and your code crashes because you try to insert something at index which is higher than the maximum index of the array - which is 0.

The line to blame is:

restaurante.imagem.insert(pickedImage, atIndex: imagemEscolhida-1)

Upvotes: 1

Related Questions