Ios 如何检索存储在firebase中的图像以在视图图像视图中显示它
我面临着如何检索存储在firebase中的图像的问题 以下是我用来存储图像的代码:Ios 如何检索存储在firebase中的图像以在视图图像视图中显示它,ios,swift,firebase,firebase-storage,Ios,Swift,Firebase,Firebase Storage,我面临着如何检索存储在firebase中的图像的问题 以下是我用来存储图像的代码: @IBAction func AddDeviceButton(sender: AnyObject) { if DeviceName.text == "" || Description.text == "" || ImageView.image == nil { let alert = UIAlertController(title: "عذرًا", message:"يجب عليك ت
@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
@IBAction func AddDeviceButton(发送方:AnyObject){
如果DeviceName.text==“”| | Description.text==“”| | ImageView.image==nil{
let alert=UIALERTCONLER(标题:“警报控制器”,消息:“警报控制器”,首选样式)
addAction(UIAlertAction(标题:“نعم”,样式:。默认值){uIn})
self.presentViewController(警报,动画:true){}
}否则{
让imageName=nsuid().UUIDString
让storageRef=FIRStorage.storage().reference().child(“设备\图像”).child(\(imageName.png))
let metaData=FIRStorageMetadata()
metaData.contentType=“image/png”
如果让uploadData=UIImagePNGRepresentation(self.ImageView.image!){
storageRef.putData(上传数据,元数据:元数据,完成:{(数据,错误)在
如果错误!=nil{
打印(错误)
}否则{
打印(“成功上传图像”)
让profileImageUrl=data?.downloadURL()?.absoluteString
//
设DeviceInfo=[
“ImageUrl”:profileImageUrl!,
“DeviceName”:self.DeviceName.text!,
“描述”:self.Description.text!,
“类别”:自选择项
]
让设备信息=[
“ImageUrl”:profileImageUrl!,
“DeviceName”:self.DeviceName.text!,
“描述”:self.Description.text!,
“类别”:自选择项,
“名称”:self.globalUserName,
“电子邮件”:self.globalEmail,
“城市”:自我、全球城市,
“电话”:self.globalPhone
]
self.ref.child(“设备”).child(FIRAuth.auth()!.currentUser!.uid)。observeSingleEventOfType(.Value,withBlock:{(快照)在
如果snapShot.exists()存在{
让numberOfDevicesAlreadyInTheDB=snapShot.childrenCount
如果设备数量已在DB中显示<3{
让newDevice=String(“Device\(numberofdevicesallreadyinthedb+1)”)
让userDeviceRef=self.ref.child(“设备”).child(FIRAuth.auth()!.currentUser!.uid)
userDeviceRef.observeSingleEventOfType(.Value,带块:{(userDevices)in
如果让userDeviceDict=userDevices.value为?NSMutableDictionary{
setObject(DeviceInfo,forKey:newDevice)
userDeviceRef.setValue(userDeviceDict)
}
})
}
否则{
let alert=UIALERTCONLER(标题:“警报控制器”,信息:“警报控制器”,首选警报样式)
addAction(UIAlertAction(标题:“نعم”,样式:。默认值){uIn})
self.presentViewController(警报,动画:true){}
}
}否则{
self.ref.child(“设备”).child(FIRAuth.auth()!.currentUser!.uid).setValue([“设备1”:设备信息])
self.ref.child(“用户设备”).childByAutoId().setValue(设备信息)
}
})
//
} })
}
}//大的还是大的
}//添加设备按钮
我只想将图像从firebase存储加载到用户配置文件中,以便用户每次登录其配置文件时都可以看到他上载到应用程序的所有图像我们强烈建议同时使用firebase存储和firebase实时数据库来完成此任务。下面是一个完整的示例: 共享:
// 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)
})
})
有关更多信息,请参阅,它是,有关如何执行此操作的实际示例。任何人都可以提供帮助??