我想知道是否可以从Firebase数据库中获取图像?我知道我可以使用存储,但是对于我的特殊情况,它需要进行大量修改,如果我可以将信息输入JSON树并以这种方式获取它将会更容易。
简而言之,我的问题是我是否可以使用Firebase存储部分中的URL并将其复制并粘贴到JSON树中的子项中,并将其检索为数据库中的任何其他数字或字符串?
每当我编写代码时,它都会尝试解开nil值,这意味着它没有找到URL所指向的图像,我认为。
提前致谢。这是我正在使用的Fetch代码:
let newsimage1 = cell.viewWithTag(3) as! UIImageView
let fetch2 = BASE_URL.child("/AA News Feed 1/Image")
fetch2.observeEventType(.Value, withBlock: { snapshot in
let base64EncodedString = snapshot.value
let imageData = NSData(base64EncodedString: base64EncodedString as! String,
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
let decodedImage = UIImage(data:imageData!)!
newsimage1.image = decodedImage
}, withCancelBlock: { error in
print(error.description)
})
答案 0 :(得分:0)
您建议的方法通常是从Firebase检索图像的方式。您只需将网址存储在FirebaseDatabase
中,然后使用该网址从FirebaseStorage
中检索图片。
常规设置
var storageRootRef: FIRStorageReference!
var databaseRootRef: FIRDatabaseReference!
override func viewDidLoad()
{
super.viewDidLoad()
storageRootRef = FIRStorage.storage().reference()
databaseRootRef = FIRDatabase.database().reference()
}
图片保存
首先需要将图片保存到FirebaseStorage
然后检索其downloadURL
位置,然后才能继续将此信息写入FirebaseDatabase
,如下所示
Swift 2
func createUserToDatabase()
{
storageRootRef = storageRootRef.child("ProfileImages").child(self.currentUser.uid + ".png")
if let imageData = UIImagePNGRepresentation(currentUser.profileImage)
{
storageRootRef.putData(imageData, metadata: nil, completion: { (metadata: FIRStorageMetadata?, error: NSError?) in
if let storageError = error
{
print("Firebase Upload Error")
print(storageError.localizedDescription)
return
}
else if let storageMetadata = metadata
{
if let imageURL = storageMetadata.downloadURL()
{
self.currentUser.profileImageURL = imageURL.absoluteString
// TODO: Now you may write to your Firebase Database since you already have the imageURL stored.
}
}
})
}
}
Swift 3
func createUserToDatabase()
{
storageRootRef = storageRootRef.child("ProfileImages").child(self.currentUser.uid + ".png")
if let imageData = UIImagePNGRepresentation(currentUser.profileImage)
{
storageRootRef.put(imageData, metadata: nil, completion: { (metadata: FIRStorageMetadata?, error: Error?) in
if let storageError = error
{
print("Firebase Upload Error")
print(storageError.localizedDescription)
return
}
else if let storageMetadata = metadata
{
if let imageURL = storageMetadata.downloadURL()
{
self.currentUser.profileImageURL = imageURL.absoluteString
// TODO: Now you may write to your Firebase Database since you already have the imageURL stored.
}
}
})
}
}
图片检索
只需在FirebaseDatabase
中查询imageURL
,然后启动URLSession
进行检索,如下所示。
Swift 2
func retrieveUserData()
{
databaseRootRef!.child("path").observeSingleEventOfType(
.Value) { (snapshot: FIRDataSnapshot) in
if let firebaseValue = snapshot.value
{
self.currentUser.profileImageURL = firebaseValue["profileImageURL"] as! String
}
let imageURL: NSURL = NSURL(string: self.currentUser.profileImageURL)!
NSURLSession.sharedSession().dataTaskWithURL(imageURL, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) in
if let sessionError = error
{
print("Error Downloading Image")
print(sessionError.localizedDescription)
}
else if let sessionData = data
{
dispatch_async(dispatch_get_main_queue(), {
self.currentUser.profileImage = UIImage(data: sessionData)!
})
}
}).resume()
}
}
Swift 3
func retrieveUserData()
{
databaseRootRef!.child("path").observeSingleEvent(
of: .value) { (snapshot: FIRDataSnapshot) in
if let firebaseValue = snapshot.value as? [String:AnyObject]
{
self.currentUser.profileImageURL = firebaseValue["profileImageURL"] as! String
}
let imageURL: URL = URL(string: self.currentUser.profileImageURL)!
URLSession.shared.dataTask(with: imageURL, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) in
if let sessionError = error
{
print("Error downloading image")
print(sessionError.localizedDescription)
}
else if let sessionData = data
{
DispatchQueue.main.async(execute: {
self.currentUser.profileImage = UIImage(data: sessionData)!
})
}
}).resume()
}
}
修改强>
FirebaseStorage
不再允许您将文件直接添加到根目录中,因此建议您创建目录以存储图像。出于独特的目的,您的图像名称可以是{{1}格式或者你可以做我做的var imageName: String = NSUUID().UUIDString + ".png"
。
在上传之前,建议您首先压缩图片,以便更快地查询和检索。