我面临的问题是如何检索存储在firebase中的图像这里是我用来存储图像的代码:
@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
我只是想从firebase存储加载图像到用户配置文件,这样每次用户登录他的配置文件时,他都可以看到他上传到应用程序的所有图像
1 回答
我们强烈建议您同时使用Firebase存储和Firebase实时数据库来完成此任务 . 这是一个完整的例子:
共享:
上传:
下载:
有关更多信息,请参阅Zero to App: Develop with Firebase,它是associated source code,以获取有关如何执行此操作的实际示例 .