使用Firebase自定义多个UIImagePicker

时间:2017-06-20 16:43:06

标签: arrays swift firebase firebase-realtime-database firebase-storage

我有一个图像数组var images: NSArray! = [],它使用自定义UIImagePicker收集最多五个图像。我需要将图像发送到Firebase,但Firebase不接受数组,因此我将数组的每个索引转换为自己的UIImage:

let imageOne = images[0] as? UIImage
let imageTwo = images[1] as? UIImage
let imageThree = images[2] as? UIImage
let imageFour = images[3] as? UIImage
let imageFive = images[4] as? UIImage

然后我创建了一个将图像发送到firebase的函数:

func uploadImage(image: UIImage){
    var randomName = randomStringWithLength(length: 5)
    let imageData = UIImageJPEGRepresentation(imageOne!, 1.0)
    let imageDatas = UIImageJPEGRepresentation(imageTwo!, 1.0)
    let imageDataThree = UIImageJPEGRepresentation(imageThree!, 1.0)
    let imageDataFour = UIImageJPEGRepresentation(imageFour!, 1.0)
    let imageDataFive = UIImageJPEGRepresentation(imageFive!, 1.0)


    let uploadRef = FIRStorage.storage().reference().child("images/\(randomName).jpg")
    guard let ImageData = imageData else { return }
    uploadRef.put(ImageData, metadata: nil) { metadata,
        error in
        if error == nil {
            print("successfully uploaded Image")

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

            randomName = randomStringWithLength(length: 5)


            let uploadRef2 = FIRStorage.storage().reference().child("images/\(randomName).jpg")
            guard let Imagedatas = imageDatas else { return }
            uploadRef2.put(Imagedatas, metadata: nil) { metadata,
                error in
                if error == nil {
                    self.imageFileNameTwo = "\(randomName as String).jpg"
                } else{
                    print("Error uploading image")
                }
                randomName = randomStringWithLength(length: 5)


                let uploadRef3 = FIRStorage.storage().reference().child("images/\(randomName).jpg")
                guard let imageDataThrees = imageDataThree else { return }
                uploadRef3.put(imageDataThrees, metadata: nil) { metadata,
                    error in
                    if error == nil {
                        self.imageFileNameThree = "\(randomName as String).jpg"
                    } else{
                        print("Error uploading image")
                    }
                    randomName = randomStringWithLength(length: 5)


                    let uploadRef4 = FIRStorage.storage().reference().child("images/\(randomName).jpg")
                    guard let imageDataFours = imageDataFour else { return }
                    uploadRef4.put(imageDataFours, metadata: nil) { metadata,
                        error in
                        if error == nil {
                            self.imageFileNameFour = "\(randomName as String).jpg"
                        } else{
                            print("Error uploading image")
                        }

                        randomName = randomStringWithLength(length: 5)


                        let uploadRef5 = FIRStorage.storage().reference().child("images/\(randomName).jpg")
                        guard let imageDataFives = imageDataFive else { return }
                        uploadRef5.put(imageDataFives, metadata: nil) { metadata,
                            error in
                            if error == nil {
                                AppDelegate.instance().dismissActivityIndicator()
                                self.imageFileNameFive = "\(randomName as String).jpg"
                            } else{
                                print("Error uploading image")
                            }

            let postObject: Dictionary<String, Any> = [

                "image" : self.imageFileName,
                "imageTwo" : self.imageFileNameTwo,
                "imageThree" : self.imageFileNameThree,
                "imageFour" : self.imageFileNameFour,
                "imageFive" : self.imageFileNameFive,


            ]

            FIRDatabase.database().reference().child("posts").child(self.loggedInUser!.uid).child(key).setValue(postObject)
          }}}}} else{
            print("Error uploading image")

        }}



}


if let pickedImageFirst = imageOne,
let pickedImageSecond = imageTwo,
let pickedImageThird = imageThree,
let pickedImageFour = imageFour,
let pickedImageFive = imageFive {
uploadImage(image: pickedImageFirst)

此功能要求用户始终有5张图像,否则会崩溃。但是,我希望它是可选的。这样用户可以在一个或五个图像之间进行选择。现在如果我选择少于5张图像,我就会崩溃:

由于未捕获的异常“NSRangeException”而终止应用程序,原因:' - [__ NSSingleObjectArrayI objectAtIndex:]:索引1超出边界[0 .. 0]'

1 个答案:

答案 0 :(得分:0)

您可以使用guard语句,而不是强行将所有图像展开到一个位置。

guard let imageData = imageData else { return }
uploadRef.put(imageData, metadata: nil) { metadata,
    error in
    if error == nil {
        print("successfully uploaded Image")

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

        randomName = randomStringWithLength(length: 5)
        let uploadRef2 = FIRStorage.storage().reference().child("images/\(randomName).jpg")

        guard let imageDatas = imageDatas else { return }
        uploadRef2.put(imageDatas, metadata: nil) { metadata

通过你的功能等等。