从imagePickerController添加图像o数组

时间:2017-02-07 10:14:03

标签: ios swift

我有一个空的图像数组,如下:

var arrayOfImages : [UIImage?]?

现在我想在用户选择后将所选图像附加到数组中。但出于某种原因,它似乎并不想附加。断点所在的print语句每次都返回nil

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    let indexpathForCell = IndexPath(row: 0, section: 0)
    let uploadImagesCell = tableView.cellForRow(at: indexpathForCell) as! UploadImagesCell

    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
        arrayOfImages?.append(editedImage)
        print(arrayOfImages?[0]) //breakpoint here
    }

虽然,如果我这样做:

var arrayOfImages = [UIImage?]()

并将图像附加到数组中,它可以正常工作。随着我之后尝试使用数组实现的目标,我觉得我需要将数组作为可选项

2 个答案:

答案 0 :(得分:1)

找到后解决:Why is my image not being appended to the array?

有了这个:

var arrayOfImages : [UIImage?]? = []

似乎我需要初始化空数组

答案 1 :(得分:0)

请如下声明,确定它是可行的,它是可选的数组

var arrayOfImages : [UIImage]? = [UIImage]()