我有2個視圖,ViewOne和ViewTwo。如何在Firebase存儲中使用iOS存儲全部在1條路徑下的圖像陣列Swift
ViewOne內部有4個imageView插座和一個postButton。一旦用戶按下postButton,所有圖像將被髮送到Firebase存儲,然後從那裏他們將被檢索到顯示在ViewTwo中。
Inside ViewTwo內部還有4個ImageView插座,用於接收從ViewOne發佈到Firebase存儲的圖像。
使用Firebase存儲我還是看不出在同一路徑下存儲多個映像。我瞭解如何將每個帖子的1張圖片存儲到存儲中,然後訪問它,但不是多張圖片。
如何將多個圖像作爲數組存儲在1路徑下(ViewOne)?那麼我如何訪問所有這些圖像(ViewTwo)?
順便說一句我通過YouTube得到了viewTwo布賴恩Voong的真棒代碼(自定義功能):https://youtu.be/b1vrjt7Nvb0
這是圖像URL字符串將進入類。我沒有打擾到初始化。
class ImageData: NSObject{
var imageOneURL: String?
var imageTwoURL: String?
var imageThreeURL: String?
var imageFourURL: String?
}
這是imageViewOne。我沒有添加ImagePicker代表,因爲它似乎沒有必要,但我知道如何使用它們。
import Firebase
ImageViewOne:UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
@IBOutlet weak var imageViewONE: UIImageView!
@IBOutlet weak var imageViewTWO: UIImageView!
@IBOutlet weak var imageViewTHREE: UIImageView!
@IBOutlet weak var imageViewFOUR: UIImageView!
var images = [ImageData]()
@IBAction func postButton(sender: UIButton){
//Unigue string for the images to be loaded under their own path
let uniqueImageIDPath = NSUUID().UUIDString
//Firebase Storage Ref
let storageRef = FIRStorage.storage().reference().child("images/\(uniqueImageIDPath)")
//Firebase Image to be stored. This is where I need help to store all the images at once.
if let myImages = UIImageJPEGRepresentation(self.imageViewONE.image!, 0.1){
storageRef.putData(myImages, metadata: nil, completion: {
(metadata, error) in
if error != nil{
print(error?.localizedDescription)
}
if let myImageString = metadata?.downloadURL()?.absoluteString{
let imageData = ImageData()
imageData.imageOne = myImageString
self.images.apped(imageData)
let values = ["imageOneURL": imageData.imageOne, "imageTwo":"?", "imageThree":"?", "imageFour":"?"]
let ref = FIRDatabase.database().reference().child("users")
ref.updateChildValues(values, withCompletionBlock: {
(error, user) in
if error != nil{
print(error?.localizedDescription)
}
})
}
}
}
這是viewTwo
import Firebase
ViewTwo: UIViewController{
@IBOutlet weak var imageViewONE: UIImageView!
@IBOutlet weak var imageViewTWO: UIImageView!
@IBOutlet weak var imageViewTHREE: UIImageView!
@IBOutlet weak var imageViewFOUR: UIImageView!
var imageOne: UIImage?
var imageTwo: UIImage?
var imageThree: UIImage?
var imageFour: UIImage?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let ref = FIRDatabase.database().reference().child("users")
ref.observeEventType(.ChildAdded, withBlock: {
(snapshot) in
if let dict = snapshot.value as? [String: AnyObject]{
let imageURLStr = dict["imageOneURL"] as? String
self.loadImageUsingCacheWithURLString(imageURLStr)
}
}
//Custom Function
func loadImageUsingCacheWithURLString(urlString: String){
let url = NSURL(string: urlString)
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: {
(data, response, error) in
if error != nil {
print(error?.localizedDescription)
return
}
dispatch_async(dispatch_get_main_queue(), {
if let displayImage = UIImage(data: data!){
self.imageOne = displayImage!
self.imageViewOne.image = self.imageOne
}
})
}).resume()
}
}