我要制作一个类似公共照片库的应用。没有错误,但无法将照片上传到Firebase Storage
我也有登录系统。
这是我的代码
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)
}
}