gpt4 book ai didi

ios - 转换到 UIImage 的 PFFile 数组在 for 循环之外为空

转载 作者:行者123 更新时间:2023-11-28 08:21:00 27 4
gpt4 key购买 nike

我试图下载一些存储在解析服务器上的图像作为 PFFile,然后在 UIImage 中转换它们。这包含在 for 循环中,并且 UIImages 附加到数组 tempImageArray。该数组在 for 循环内填充(包含服务器上的所有 9 个图像),但是当我尝试在 for 循环外使用该数组时,该数组为空,返回 []。

这是否与 getdatainbackground 有关,而 swift 在我可以使用数组 tempImagesArray 之前跳过了它?

整个 block 的代码如下。

let packQuery = PFQuery(className: "Pack")

packQuery.findObjectsInBackground(block: { (objectsArray, error) in
if error != nil {
print(error!)
} else if let packs = objectsArray {
var tempImageArray = [UIImage]()
for object in packs {
if object.object(forKey: "file") != nil {
var imageDataDownLoad = object["file"] as! PFFile
imageDataDownLoad.getDataInBackground(block: { (imageData, error) in
if error == nil {
if let image = UIImage(data: imageData!) {
tempImageArray.append(image)
print(tempImageArray)
// this returns a full array
}
}
})
}
}
//self.packImage.append(tempImageArray)
print(tempImageArray)
//this returns []
self.collectionView?.reloadData()
}
})

}

------------ 编辑--------------------使用 GitHub 上可用的 parseUI 中的 PFImageView

packQuery.findObjectsInBackground(block: { (objectsArray, error) in
if error != nil {
print(error!)
} else if let packs = objectsArray {

for object in packs {

let arrayName = object.object(forKey: "packName") as! String
let arrayDescription = object.object(forKey: "packDesctription") as! String
let arrayTitle = object.object(forKey: "packTitle") as! String
let arrayImage = object.object(forKey: "file") as! PFFile

self.packArray.append(packStruct(packName: arrayName, packDescription: arrayDescription, packTitle: arrayTitle, packImage: arrayImage))
}
self.collectionView?.reloadData()
}
})

最佳答案

    packQuery.findObjectsInBackground(block: { (objectsArray, error) in
if error != nil {
print(error!)
} else if let packs = objectsArray {

for object in packs {

let arrayName = object.object(forKey: "packName") as! String
let arrayDescription = object.object(forKey: "packDesctription") as! String
let arrayTitle = object.object(forKey: "packTitle") as! String
let arrayImage = object.object(forKey: "file") as! PFFile

self.packArray.append(packStruct(packName: arrayName, packDescription: arrayDescription, packTitle: arrayTitle, packImage: arrayImage))
}
self.collectionView?.reloadData()
}
})

关于ios - 转换到 UIImage 的 PFFile 数组在 for 循环之外为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41290379/

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