我试图将存储在解析服务器上的一些图像下载为PFFile,然后在UIImage中转换它们。它包含在for循环中,UIImages附加到数组tempImageArray。此数组填充在for循环中(包含服务器上的所有9个映像)但是当我尝试在for循环外使用此数组时,数组为空,返回[]。
这与getdatainbackground有什么关系,swift在我可以使用数组tempImagesArray之前跳过它?
整个块的代码如下。
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()
}
})
}
----------- EDIT -------------------- 使用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()
}
})
答案 0 :(得分:0)
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()
}
})