Ios 集合视图Firebase不工作
我要做一个像公共照片库一样的应用程序。没有错误,但无法将照片上载到Ios 集合视图Firebase不工作,ios,swift,firebase,firebase-realtime-database,firebase-storage,Ios,Swift,Firebase,Firebase Realtime Database,Firebase Storage,我要做一个像公共照片库一样的应用程序。没有错误,但无法将照片上载到Firebase存储我也有登录系统 这是我的密码 import UIKit import Firebase import MobileCoreServices class publicAlbumViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLay
Firebase存储
我也有登录系统
这是我的密码
import UIKit
import Firebase
import MobileCoreServices
class publicAlbumViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet var collectionView: UICollectionView!
let imagePicker = UIImagePickerController()
var value = 1
var imageArray:[UIImage] = [UIImage(named: "second")!]
var metadata = FIRStorageMetadata()
override func viewDidLoad() {
super.viewDidLoad()
let albumRef = FIRDatabase.database().reference().child("albumNum")
albumRef.observeEventType(.Value) { (snap: FIRDataSnapshot) in
print(snap.value as! Int)
self.value = snap.value as! Int
}
let albumImageRef = FIRStorage.storage().reference().child("albumNum")
for num in 1...value {
albumImageRef.child("\(num)").dataWithMaxSize(Int64.max, completion: {(data, error) in
if error == nil {
self.imageArray.append(UIImage(data: data!)!)
}
})
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return value
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("publicAlbumCell", forIndexPath: indexPath) as! AlbumCollectionViewCell
cell.imageView?.image = self.imageArray[indexPath.row]
return cell
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let albumImageRef = FIRStorage.storage().reference().child("albumNum")
self.dismissViewControllerAnimated(true, completion: nil)
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let newImage = UIImageJPEGRepresentation(image, 3.0)
self.metadata.contentType = "image/jpeg"
albumImageRef.child("\(value + 1)").putData(newImage!, metadata: self.metadata){metadata, error in
if error == nil {}
}
}
@IBAction func plusButtonTapped(sender: AnyObject) {
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String]
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
这里有几个问题:1)对set
self.value
的调用是异步的,这意味着您的for循环可能会在1到0之间迭代,并且您实际上不会下载任何内容。您将需要执行其他操作以获取必要的文件。2) 您永远不会重新加载collectionview(self.collectionview.reloadData()
),因此即使您获得一张照片并将其添加到阵列中,您会注意到,cellforrowatinexpath
不会触发,因为您从未触发它。我强烈建议您查看并检查相关代码,了解如何在iOS上使用Firebase存储和实时数据库:)这里有几个问题:1)设置self.value
的调用是异步的,这意味着您的for循环可能会在1到0之间迭代,而您实际上不会下载任何内容。您将需要执行其他操作以获取必要的文件。2) 您永远不会重新加载collectionview(self.collectionview.reloadData()
),因此即使您获得一张照片并将其添加到阵列中,您会注意到,cellforrowatinedexpath
不会触发,因为您从未触发它。我强烈建议您查看并检查相关代码,了解如何在iOS上使用Firebase存储和实时数据库:)