我正在尝试将PFFile转换为UIImage。然而,它似乎要到最后才能运行。在图像被调用后的行将图像追加到数组(photosArray.append( image !)),但是当我这样做时,它就不工作了,并且什么也不存储。我相信这是因为图像还没有被完全检索,因为它是在获取背景中的图像。我如何不让它保存在后台,但等到所有数据加载,然后附加到photosArray图像,所以数组不是空的?
var photosArray = [UIImage]()
let photosValue = log["Images"] as! PFFile
photosValue.getDataInBackgroundWithBlock({
(imageData: NSData?, error:NSError?) -> Void in
if (error == nil) {
let image = UIImage(data:imageData!)
//line below appends nothing! photosArray is an empty array, []
photosArray.append(image!)
//following line of code doesnt run until the end. Is the last line to be printed in console.
println("Our Image: \(image)")
}
})
//prints "[]", no value
println(photosArray)
println("Our Image Array^^")
}
//after all the code is ran, the println("Our Image: \(image)") will run,
//and say "Our Image: Optional(<UIImage: 0x16f0ead0>, {394, 256})".
//This is the last line of the console logged, so it IS saving, but not until the end.发布于 2015-07-23 20:30:14
这不是一个正确的假设,getDataInBackgroundWithBlock的全部目的是处理响应数据。你也不能分配
let image = UIImage(data: imageData!)如果是零。
我不确定您所指的println语句是什么,但是在后台仍在收集数据时打印出数组可能不会给出结果,只是
[]但你应该看看
println("Our image: \(image)")如果您计划在UIImageView中显示这些图像,我建议查看ParseUI类PFImageView,它有一个属性,您可以为它分配一个PFFile,然后是一个加载它的方法。
https://stackoverflow.com/questions/31597255
复制相似问题