当我尝试从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!)
})
}
}
}
我将它们分成两个单独的函数,因为我试图在一个函数中管理它们,并且顺序也很混乱,我认为这可能有效,但显然不是。
答案 0 :(得分:2)
不是将数据存储在数组中,而是将其存储在字典中。密钥可以是数字i
,或者您在使用时想要引用图像。
答案 1 :(得分:0)
我建议采用类似于Storing multiple images into firebase and getting urls的方法,将URL存储在实时数据库中,并将其用作订购,展示等的真实来源。它更容易,并使为更好的应用程序:)