解码 Firebase base64 Swift 2.0
Decoding Firebase base64 Swift 2.0
我正在尝试加载存储在 Firebase 服务器中的图像。我正在检查其他答案,但我找不到为什么在能够打印正确的字符串值后得到 nil 的原因:
在我的应用程序中,我有以下数据结构:
这是我用来加载它的代码(nil 值是评论中显示的值)
let profileDetailsRef = self.ref.childByAppendingPath("users/" + sessionUserID + "/details")
profileDetailsRef.observeEventType(.Value, withBlock: { snapshot in
self.txtUsername.text = sessionUserID
self.txtDisplayName.text = snapshot.value.objectForKey("nameToDisplay") as? String
let base64EncodedString = snapshot.value.objectForKey("userImage")! as! String
print(base64EncodedString) //Here it's printing well because I get the same value as shows in the dasboard so it finds it
//But the value "decodedData" here below is returning **nil**
let decodedData = NSData(base64EncodedString: base64EncodedString as! String, options: NSDataBase64DecodingOptions())
let decodedImage = UIImage(data: decodedData!)!
self.imageUserImage.image = decodedImage
}, withCancelBlock: { error in
print(error.description)
})
谢谢!
使用像 Cloudinary 这样的托管 API 的图像并简单地将 URL 存储在 Firebase 中可能会容易得多。
也就是说,问题很可能是您的编码和解码选项不匹配。尝试在每个选项字段中提供 []
。例如,要编码:
let image = [#Image(imageLiteral: "icon.png")#] // your UIImage
let imageData = UIImagePNGRepresentation(image)! // or however you store your image
let base64EncodedString = imageData.base64EncodedStringWithOptions([])
并解码:
if let decodedData = NSData(base64EncodedString: base64EncodedString, options: []),
let decodedImage = UIImage(data: decodedData) {
// do something with decodedImage
}
如果这不起作用,Firebase 可能会截断您的图像,因为字符串长度超出了限制。
这种方法不是存储和加载图像的好方法——你会错过 HTTP 缓存、内容类型、服务器端优化等——所以如果可以的话我会尽量避免。
我正在尝试加载存储在 Firebase 服务器中的图像。我正在检查其他答案,但我找不到为什么在能够打印正确的字符串值后得到 nil 的原因:
在我的应用程序中,我有以下数据结构:
这是我用来加载它的代码(nil 值是评论中显示的值)
let profileDetailsRef = self.ref.childByAppendingPath("users/" + sessionUserID + "/details")
profileDetailsRef.observeEventType(.Value, withBlock: { snapshot in
self.txtUsername.text = sessionUserID
self.txtDisplayName.text = snapshot.value.objectForKey("nameToDisplay") as? String
let base64EncodedString = snapshot.value.objectForKey("userImage")! as! String
print(base64EncodedString) //Here it's printing well because I get the same value as shows in the dasboard so it finds it
//But the value "decodedData" here below is returning **nil**
let decodedData = NSData(base64EncodedString: base64EncodedString as! String, options: NSDataBase64DecodingOptions())
let decodedImage = UIImage(data: decodedData!)!
self.imageUserImage.image = decodedImage
}, withCancelBlock: { error in
print(error.description)
})
谢谢!
使用像 Cloudinary 这样的托管 API 的图像并简单地将 URL 存储在 Firebase 中可能会容易得多。
也就是说,问题很可能是您的编码和解码选项不匹配。尝试在每个选项字段中提供 []
。例如,要编码:
let image = [#Image(imageLiteral: "icon.png")#] // your UIImage
let imageData = UIImagePNGRepresentation(image)! // or however you store your image
let base64EncodedString = imageData.base64EncodedStringWithOptions([])
并解码:
if let decodedData = NSData(base64EncodedString: base64EncodedString, options: []),
let decodedImage = UIImage(data: decodedData) {
// do something with decodedImage
}
如果这不起作用,Firebase 可能会截断您的图像,因为字符串长度超出了限制。
这种方法不是存储和加载图像的好方法——你会错过 HTTP 缓存、内容类型、服务器端优化等——所以如果可以的话我会尽量避免。