gpt4 book ai didi

ios - 在 Swift 中选择照片时出现 "Array index out of range"

转载 作者:行者123 更新时间:2023-11-28 11:15:46 25 4
gpt4 key购买 nike

我不知道这里有什么问题。

我有 6 张图片,当我点击它们时,它会弹出一个图片选择器。如果我选择一张有顺序的照片,比如 1,2,3,4,5,6... 就可以了。但是当我选择不是第一张的第一张照片(数组[0])时,我得到一个错误:

`$"Array index out of range"

这是我的代码:

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
}


}

最佳答案

在以正确的大小使用数组之前,您应该先对其进行初始化。我想引用我对一个非常相似的答案 question .因此,在定义时将其从 0 初始化为 5,然后您的代码应该可以工作。

关于ios - 在 Swift 中选择照片时出现 "Array index out of range",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31920233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com