ios - 从图像数组中快速删除图像

标签 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

中调用该函数

最佳答案

虽然我不同意您的处理方式,但这将回答您的问题:

首先更改var images: NSArray! = []var images: NSMutableArray! = []

while images.count < 5{
    images.addObject(UIImage())
}
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 }

关于ios - 从图像数组中快速删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44682024/

相关文章:

iphone - -(bool) 不返回

ios - swift 4 : "Non-nominal type ' T' does not support explicit initialization"

ios - UIScrollView 的 subview 保持在同一位置,而 UIScrollView 垂直滚动

ios - 从通知打开应用程序时打开特定 View

ios - 在 iOS sdk 中初始化 View Controller

javascript - 如何在 Jade 中填写表格

jquery - 比较 jQuery 中关于 CSS 类的两个数组

C++ 数组和东西

ios - 为什么有时会重用对象标识符?

ios - 当应用程序进入后台时无法设置亮度,任何人都可以对此有任何想法吗?