Rya*_*zzo 5 nsdata ios firebase swift firebase-storage
我有一个应该处理下载图像的方法.除了从Firebase存储返回的数据是0字节之外,它似乎完全正常工作.为什么是这样?
func useDatabaseToDownloadPicture () {
if let userId = FIRAuth.auth()?.currentUser?.uid {
// Get a reference to the storage service using the default Firebase App
let storage = FIRStorage.storage()
let firebaseImages = FIRStorage.storage().reference().child("Images")
let userPhotoLocation = firebaseImages.child("Users").child(userId).child("profilePicture.jpg")
let myRef = FIRStorage.storage().reference()
let firebaseProfilePicLocation = firebase.child("Users").child(userId).child("Images").child("profilePicture").child("downloadURL")
firebaseProfilePicLocation.observe(.value, with: { (snapshot) in
print(" Profile Picture Databse Snapshot -> \n \(snapshot) ")
// Get download URL from snapshot
if let downloadURL = snapshot.value as? String {
print(" downloadURL converted to String ")
// Create a storage reference from the URL
let storageRef = storage.reference(forURL: downloadURL)
// Download the data, assuming a max size of 1MB (you can change this as necessary)
storageRef.data(withMaxSize: 1 * 1024 * 1024) { (data, error) -> Void in
print("Downloading Data . . . Data String -> \(String(describing: data))")
print("Downloading Data . . . Data Literal -> \(data)")
// Create a UIImage, add it to profile picture
if data != nil {
print("Data is not nil!")
if let image = UIImage(data: data!) {
print("Image Created")
} else {
print("Image Could Not create!! check data...")
let url = URL(string: downloadURL)
let urlData = try? Data(contentsOf: url!)
if let img = UIImage(data: urlData!) {
print("Image 2 Created")
} else {
print("Image 2 Failed")
}
}
let pic = UIImage(data: data!)
self.profilePicture.image = pic
} else {
print(" ? Data is nil ")
}
}
} else if let downloadURL = snapshot.value as? URL {
print(" downloadURL converted to URL! ")
// Create a storage reference from the URL
let storageRef = storage.reference(forURL: downloadURL.absoluteString)
// Download the data, assuming a max size of 1MB (you can change this as necessary)
storageRef.data(withMaxSize: 1 * 1024 * 1024) { (data, error) -> Void in
// Create a UIImage, add it to profile picture
if data != nil {
let pic = UIImage(data: data!)
self.profilePicture.image = pic
} else {
print(" ? Data is nil")
}
}
} else {
// . . . Do nothing, all ways failed
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
打印结果是:
downloadURL converted to String
Downloading Data . . . Data String -> Optional(0 bytes)
Downloading Data . . . Data Literal -> Optional(0 bytes)
Image Could Not create!! check data...
Image 2 Failed
Run Code Online (Sandbox Code Playgroud)
对我来说,似乎问题是正确的数据进入,但似乎问题是数据是0字节.为什么会这样?
谢谢!!
| 归档时间: |
|
| 查看次数: |
263 次 |
| 最近记录: |