在Parse(ios)中对UIImage的PFFile

时间:2015-07-23 20:17:03

标签: ios image swift parse-platform

我正在尝试将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.

1 个答案:

答案 0 :(得分:1)

这不是一个正确的假设,getDataInBackgroundWithBlock的重点是处理响应数据。你也无法分配

let image = UIImage(data: imageData!)

如果没有。

我不确定你所指的是什么println语句,但是在数据仍然在后台收集时打印出数组可能无法给你一个结果,只是

[]

但你应该看到

println("Our image: \(image)")

如果您计划在UIImageView中显示这些图像,我建议您查看ParseUI类PFImageView,它具有可以为其分配PFFile的属性,然后是一个加载它的方法。

相关问题