我正在尝试从Firebase Storage下载图像并将其显示在我的iOS应用中。我有以下代码:
func getSponsorImages() {
guard let name = self.name, let city = self.city else { return }
let refString = "cities/\(city)/sponsors/\(name)/photos"
ref.child(refString).observeSingleEvent(of: .value, with: { (snapshot) in
if let snapshots = snapshot.children.allObjects as? [DataSnapshot], snapshots.count != 0 {
for snap in snapshots {
let imageRef = storageRef.child("cities/\(city)/\(name)/\(snap.value!)")
imageRef.getData(maxSize: 50*1024*1024) { data, error in
if let error = error {
print("Error getting image: \(error.localizedDescription)")
} else {
guard let photoData = data else { return }
guard let photo = UIImage(data: photoData) else { return }
self.photos.append(photo!)
}
}
}
}
})
}
正在返回数据,并且没有引发任何错误,但是当我尝试从Data创建UIImage时,它崩溃了。有谁知道为什么它返回无效数据?我还确保正确设置了Firebase规则。