gpt4 book ai didi

arrays - Firebase 存储异步图像下载顺序不正确

转载 作者:行者123 更新时间:2023-11-28 12:42:09 24 4
gpt4 key购买 nike

当我尝试从 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 或者您希望在使用图像时引用它。

关于arrays - Firebase 存储异步图像下载顺序不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39192495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com