Firebase 存储异步图像下载顺序不正确
Firebase Storage Async Image Download not in the right order
当我尝试从 Firebase 存储下载一批图像时遇到了问题。基本上,因为文件大小不同,图像没有正确附加到图像数组,导致图像的顺序错误,我想要。下面是代码
import Foundation
import FirebaseStorage
class GalleryCellDetailedData {
var selectedGallery:String?
var count:Int
init(selectedGallery:String?,count:Int){
self.selectedGallery = selectedGallery
self.count = count
}
func addImages(completion:(data:[NSData])->()){
var datas = [NSData]()
let myGroup = dispatch_group_create()
for i in 0..<count {
dispatch_group_enter(myGroup)
getImage(i, completion: { (image:NSData) in
datas.append(image)
print("Finish Request \(i)")
dispatch_group_leave(myGroup)
})
}
dispatch_group_notify(myGroup, dispatch_get_main_queue(), {
completion(data: datas)
})
}
private func getImage(number:Int, completion:(image:NSData)->()){
let storage = FIRStorage.storage()
//Reference to Firebase Profile Picture Storage
let storageRef = storage.referenceForURL("gs://mannacatering-addcb.appspot.com")
print("Initiating Image Download")
let galleryPicRef = storageRef.child("Gallery/\(selectedGallery!)/g\(String(number)).jpg")
//Download Image
galleryPicRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
if (error != nil) {
print("fail to download image")
}
dispatch_async(dispatch_get_main_queue(), {
print("Dispatching image")
completion(image:data!)
})
}
}
}
我将它们分成 2 个独立的函数,因为我试图在一个函数中管理它们,而且顺序也很乱,我认为这可能有效,但显然无效。
不要将数据存储在数组中,而是将其存储在字典中。键可以是数字 i
或者您希望在使用时引用图像。
我建议采用类似于 Storing multiple images into firebase and getting urls 的方法,将 URL 存储在实时数据库中,并将其用作订购、显示等的真实来源。它更容易,而且效果更好应用:)
当我尝试从 Firebase 存储下载一批图像时遇到了问题。基本上,因为文件大小不同,图像没有正确附加到图像数组,导致图像的顺序错误,我想要。下面是代码
import Foundation
import FirebaseStorage
class GalleryCellDetailedData {
var selectedGallery:String?
var count:Int
init(selectedGallery:String?,count:Int){
self.selectedGallery = selectedGallery
self.count = count
}
func addImages(completion:(data:[NSData])->()){
var datas = [NSData]()
let myGroup = dispatch_group_create()
for i in 0..<count {
dispatch_group_enter(myGroup)
getImage(i, completion: { (image:NSData) in
datas.append(image)
print("Finish Request \(i)")
dispatch_group_leave(myGroup)
})
}
dispatch_group_notify(myGroup, dispatch_get_main_queue(), {
completion(data: datas)
})
}
private func getImage(number:Int, completion:(image:NSData)->()){
let storage = FIRStorage.storage()
//Reference to Firebase Profile Picture Storage
let storageRef = storage.referenceForURL("gs://mannacatering-addcb.appspot.com")
print("Initiating Image Download")
let galleryPicRef = storageRef.child("Gallery/\(selectedGallery!)/g\(String(number)).jpg")
//Download Image
galleryPicRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
if (error != nil) {
print("fail to download image")
}
dispatch_async(dispatch_get_main_queue(), {
print("Dispatching image")
completion(image:data!)
})
}
}
}
我将它们分成 2 个独立的函数,因为我试图在一个函数中管理它们,而且顺序也很乱,我认为这可能有效,但显然无效。
不要将数据存储在数组中,而是将其存储在字典中。键可以是数字 i
或者您希望在使用时引用图像。
我建议采用类似于 Storing multiple images into firebase and getting urls 的方法,将 URL 存储在实时数据库中,并将其用作订购、显示等的真实来源。它更容易,而且效果更好应用:)