2016-09-17 26 views
1

我面臨着如何檢索存儲在火力 這裏的圖像問題是我用來存儲圖像的代碼:存儲在火力如何檢索圖像來顯示它在查看影像

@IBAction func AddDeviceButton(sender: AnyObject) { 
    if DeviceName.text == "" || Description.text == "" || ImageView.image == nil { 
     let alert = UIAlertController(title: "عذرًا", message:"يجب عليك تعبئة معلومات الجهاز كاملة", preferredStyle: .Alert) 
     alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in }) 
     self.presentViewController(alert, animated: true){} 

    } else { 

     let imageName = NSUUID().UUIDString 
     let storageRef = FIRStorage.storage().reference().child("Devices_Images").child("\(imageName).png") 

     let metaData = FIRStorageMetadata() 
     metaData.contentType = "image/png" 

     if let uploadData = UIImagePNGRepresentation(self.ImageView.image!) { 
      storageRef.putData(uploadData, metadata: metaData, completion: { (data, error) in 
       if error != nil { 
        print(error) 

       } else { 
        print("Image Uploaded Succesfully") 
        let profileImageUrl = data?.downloadURL()?.absoluteString 

        // 

        let DeviceInfo = [ 
         "ImageUrl":profileImageUrl!, 
         "DeviceName":self.DeviceName.text!, 
         "Description":self.Description.text!, 
         "Category":self.itemSelected 
        ] 

        let DeviceInformation = [ 
         "ImageUrl":profileImageUrl!, 
         "DeviceName":self.DeviceName.text!, 
         "Description":self.Description.text!, 
         "Category":self.itemSelected, 
         "name": self.globalUserName, 
         "email":self.globalEmail , 
         "city": self.globalCity, 
         "phone": self.globalPhone 
        ] 


        self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).observeSingleEventOfType(.Value, withBlock: {(snapShot) in 
         if snapShot.exists(){ 
          let numberOfDevicesAlreadyInTheDB = snapShot.childrenCount 
          if numberOfDevicesAlreadyInTheDB < 3{ 
           let newDevice = String("Device\(numberOfDevicesAlreadyInTheDB+1)") 
           let userDeviceRef = self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid) 
           userDeviceRef.observeSingleEventOfType(.Value, withBlock: {(userDevices) in 
            if let userDeviceDict = userDevices.value as? NSMutableDictionary{ 

             userDeviceDict.setObject(DeviceInfo,forKey: newDevice) 

             userDeviceRef.setValue(userDeviceDict) 
            } 
           }) 
          } 
          else{ 
           let alert = UIAlertController(title: "عذرًا", message:"يمكنك إضافة ثلاثة أجهزة فقط كحد أقصى", preferredStyle: .Alert) 
           alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in }) 
           self.presentViewController(alert, animated: true){} 
          } 
         }else{ 
          self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).setValue(["Device1" : DeviceInfo]) 
          self.ref.child("UserDevices").childByAutoId().setValue(DeviceInformation) 

         } 
        }) 

        // 

       } }) 
     } 


    } //Big Big Else 

} //AddDeviceButton 

我只是想從火力存儲加載圖像,用戶配置文件,使每個用戶登錄到自己的個人資料時,他可以看到他上傳到應用程序的所有圖像

+0

http://stackoverflow.com/questions/33644560/swift2-retrieving-images-from-firebase – IvRRimUm

+0

任何人都可以幫忙? –

回答

3

我們強烈建議您使用火力地堡貯存和火力地堡實時數據庫共同完成這個。這裏有一個完整的例子:

共享:

// Firebase services 
var database: FIRDatabase! 
var storage: FIRStorage! 
... 
// Initialize Database, Auth, Storage 
database = FIRDatabase.database() 
storage = FIRStorage.storage() 
... 
// Initialize an array for your pictures 
var picArray: [UIImage]() 
let myUserId = ... // get this from Firebase Auth or some other ID provider 

上傳:

let fileData = NSData() // get data... 
let storageRef = storage.reference().child("userFiles/\(myUserId)/myFile") 
storageRef.putData(fileData).observeStatus(.Success) { (snapshot) in 
    // When the image has successfully uploaded, we get it's download URL 
    let downloadURL = snapshot.metadata?.downloadURL()?.absoluteString 
    // Write the download URL to the Realtime Database 
    let dbRef = database.reference().child("userFiles/\(myUserId)/myFile") 
    dbRef.setValue(downloadURL) 
} 

下載:

let dbRef = database.reference().child("userFiles/\(myUserId)") 
dbRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in 
    // Get download URL from snapshot 
    let downloadURL = snapshot.value() as! String 
    // Create a storage reference from the URL 
    let storageRef = storage.referenceFromURL(downloadURL) 
    // Download the data, assuming a max size of 1MB (you can change this as necessary) 
    storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in 
    // Create a UIImage, add it to the array 
    let pic = UIImage(data: data) 
    picArray.append(pic) 
    }) 
}) 

欲瞭解更多信息,請參閱Zero to App: Develop with Firebase,它的associated source code,對於實際如何做到這一點的例子。