gpt4 book ai didi

arrays - 使用 Firebase 自定义多个 UIImagePicker

转载 作者:行者123 更新时间:2023-11-30 12:22:13 25 4
gpt4 key购买 nike

我有一个图像数组 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]”

最佳答案

您可以使用 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

通过您的函数依此类推。

关于arrays - 使用 Firebase 自定义多个 UIImagePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44658759/

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