【问题标题】:PFFile to UIImage in Parse (ios)PFFile 到解析中的 UIImage (ios)
【发布时间】:2015-07-23 20:17:03
【问题描述】:

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

【问题讨论】:

    标签: ios image swift parse-platform


    【解决方案1】:

    这不是一个正确的假设,getDataInBackgroundWithBlock 的全部意义在于处理响应数据。你也不能分配

    let image = UIImage(data: imageData!)
    

    如果它是零。

    我不确定您指的是什么 println 语句,但是在后台仍在收集数据时打印出数组可能不会给您结果,而只是

    []
    

    但你应该看到

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

    如果您打算在 UIImageView 中显示这些图像,我建议您查看 ParseUI 类 PFImageView,它有一个属性,您可以将 PFFile 分配给然后一个方法来加载它。

    【讨论】:

    • 你是对的。图像仍然填充视图控制器。我猜我不应该 100% 依赖 println,哈哈。
    • 大声笑,是Parse后台线程,一开始感觉很奇怪,但很快就会变成第二自然。
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2015-07-30
    • 1970-01-01
    相关资源
    最近更新 更多