我具有以下imagePickerController
委托函数,每次选择图像时,它将上载图像的前urlString
替换为Firebase Storage
,而不是插入位于倒数第二个array.count-在cell
数组中的1个位置(根据设计,我使用最后一个条目将collectionview
中的另一个images
出队)。结果最后一张图片urlString总是被最新一张替换。
我必须添加,如果我一次从当前VC导航离开,然后一次返回并再次上传另一张图片,则一次上传一张图片,那么它将按预期工作。
代码中的打印语句的结果如下:
上传图片1:
0. ADDING images.count: 3
1. ADDING images.count: 4
2. ADDING images.count: 4
images.last item: Optional("dummy string")
上传图片2(图片数量未增加):
0. ADDING images.count: 4
1. ADDING images.count: 4
2. ADDING images.count: 4
images.last item: Optional("dummy string")
功能:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
self.dismiss(animated: true, completion: nil)
Utilities.run.showSVHUD(uiView: self.view, status: "Saving")
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromPicker = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
guard let uid = Auth.auth().currentUser?.uid else { return }
print("0. ADDING images.count: \(self.images.count)")
DataService.run.uploadProfileImageToFirebaseStorage(image: selectedImage) { (success, urlString) in
self.images.insert(urlString, at: self.images.count - 1)
print("1. ADDING images.count: \(self.images.count)")
let slice = self.images.dropLast()
let urlArray = Array(slice) //Converting Slice to Array
let dictionary = urlArray.indexedDictionary //converting to Dictionary
DataService.run.updateProfileImagesValueForUser(uid: uid, imagesUrls: dictionary, handler: { (success) in
if success {
Utilities.run.dismissSVHUD(delay: 0.5)
print("2. ADDING images.count: \(self.images.count)")
print("images.last item: \(String(describing: self.images.last))")
self.collectionView.reloadData()
self.collectionView.layoutIfNeeded()
}//end if success
})// end updateProfileImagesValueForUser
}//end uploadProfileImageToFirebaseStorage
}//end if let selectedImage = selectedImageFromPicker
}//end func
images
数组是从传递的User
对象开始的,其中包含String
个对象的数组。
func initData(forUser user:User) {
self.user = user
}//end func
setupviews()
在viewWillAppear()
中被调用,从而初始化images
数组
func setupViews(){
images = user!.profileImages
images.append("dummy string")
let imageString = user!.profilePictureURL
let url = URL(string: imageString)//create URL from string
profileImg.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "placeholder"), options: [.continueInBackground])
}//end func