从图像阵列swift中删除图像

时间:2017-06-21 16:54:46

标签: ios arrays swift

我有一个图像数组var images: NSArray! = []我的数组中的最大图像数是5个图像。我希望能够使用任意数量的图像,所以我提取了它们

guard let imageOne = images[0] as? UIImage else { return }
guard let imageTwo = images[1] as? UIImage else { return }
guard let imageThree = images[2] as? UIImage else { return }
guard let imageFour = images[3] as? UIImage else { return }
guard let imageFive = images[4] as? UIImage else { return }

我希望每个索引都是可选的,所以我可以使用imageOne或imageOne和imageTwo但是当我尝试使用少于5个图像时,我得到一个索引错误。如何使每个索引可选,以便它不再与数组分组,并且每个索引都成为自己的图像。

我最初有一个数组,因为我有一个只接受数组的自定义UIImagePicker,但我想将图像上传到Firebase,而Firebase不接受数组,所以我必须从数组中提取图像。

使用下面的建议答案,我对我的代码执行了此操作:

        for (counter, img) in images.enumerated(){
                if counter == 0 {

                    func uploadImage(image: UIImage){
                        var randomName = randomStringWithLength(length: 5)
                        let imageData = UIImageJPEGRepresentation(imageOne!, 1.0)


                        let uploadRef = FIRStorage.storage().reference().child("images/\(randomName).jpg")
                        uploadRef.put(img as! UIImage, metadata: nil) { metadata,
                            error in
                            if error == nil {
                                print("successfully uploaded Image")

                                self.imageFileName = "\(randomName as String).jpg"

                                randomName = randomStringWithLength(length: 5)

                                let postObject: Dictionary<String, Any> = [
                                    "image" : self.imageFileName,

                                ]

                                FIRDatabase.database().reference().child("posts").child(self.loggedInUser!.uid).child(key).setValue(postObject)

                                print("Posted to Firebase. ")

                            } else{
                                print("Error uploading image")

                            }}

                    }
 if let pickedImage = img as?
                            UIImage{
                            uploadImage(image: pickedImage)

                                    }

                }
    }

现在,我如何检查选择了多少图像,并在每个if counter == num内调用该功能

2 个答案:

答案 0 :(得分:2)

虽然我不同意你的做法,但这将回答你的问题:

首先将class ContactsController: UITableViewController { // some code var contacts = try! Realm().objects(Contact.self).filter("isDeleted == false").sorted(by: ["firstLetter","fullName"]) var sections: [String] { return Set(contacts.value(forKeyPath: "firstLetter") as![String]).sorted() } override func viewDidLoad() { super.viewDidLoad() setUI() } // another code } 更改为var sections: [String] { return Set(contacts.value(forKeyPath: "firstLetter") as![String]).sorted { //Favourites sorts before all else if $0 == "FAVORITES" { return true } if $1 == "FAVORITES" { return false } // Symbols sort after all else if $0 == "#" { return false } if $1 == "#" { return true } return $0 < $1 } }

var images: NSArray! = []

答案 1 :(得分:1)

for img in imgs  {
    //do something for each img
}

不要将单个数组成员复制到变量中。通过迭代列表,您将对列表中的每个图像执行某些操作,而不会出现任何索引错误。如果您需要以不同方式处理每个图像,可以使用带计数器的条件分支。

let counter = 0
for img in imgs  {
    if counter == 0 {
        // do something for first img
    }
    if counter == 1 {
        // do something for second img
    }
    //...
    if counter == 4 {
        // do something for fifth img
    }
    counter += 1
}