检索多个解析图像(Swift)

时间:2016-02-02 03:02:51

标签: ios database swift parse-platform

以下是我通常如何从Parse.com检索图像的示例。我现在遇到了我想从Parse检索20多个图像的情况,但我正在寻找一种更有效的方法。有人可以解释如何在代码中实现这个,以及我应该如何在Parse中存储20多个PFF?

func loadData(){
let findDataParse = PFQuery(className: "JobListing")
findDataParse.findObjectsInBackgroundWithBlock{
    (objects: [PFObject]?, error: NSError?) -> Void in
    if (error == nil) {
        for object in objects! {
           let userImageFile = object["ImageOne"] as! PFFile
            let userImageFile1 = object["ImageTwo"] as! PFFile
            let userImageFile2 = object["ImageThree"] as! PFFile
            userImageFile.getDataInBackgroundWithBlock {
                (imageData: NSData?, error: NSError?) -> Void in
                let listingImage1 = UIImage(data:imageData!)
                userImageFile1.getDataInBackgroundWithBlock {
                    (imageData1: NSData?, error1: NSError?) -> Void in
                    let listingImage2 = UIImage(data:imageData1!)
                    userImageFile2.getDataInBackgroundWithBlock {
                        (imageData2: NSData?, error1: NSError?) -> Void in
                        let listingImage3 = UIImage(data:imageData2!)

self.flyerImageLarge1.image = listingImage1
self.flyerImageLarge2.image = listingImage2
self.flyerImageLarge3.image = listingImage3
}}}}}}}

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用ParseUI获得更清晰,更高效的代码。 为此,请添加ParseUI框架。 然后,单击图像并将“类”更改为PFImageView。 You can see this here.

一旦这样做,您就可以轻松设置PFImageView的图像:

if let myServerImage = object.valueForKey("imageFromUser") as? PFFile {
        self.myImage.file = myServerImage
        self.myImage.loadInBackground()
    }

其中myServerImage是您从Parse服务器检索的图像,myImage是故事板中的图像。

在你的情况下,它将是这样的:

func loadData(){
    let findDataParse = PFQuery(className: "JobListing")
    findDataParse.findObjectsInBackgroundWithBlock{
    (objects: [PFObject]?, error: NSError?) -> Void in
    if (error == nil) {
        if let myServerImage = object.valueForKey("ImageOne") as? PFFile {
            self.flyerImageLarge1.file = myServerImage
            self.flyerImageLarge1.loadInBackground()
        }
    if let myServerImage = object.valueForKey("ImageTwo") as? PFFile {
            self.flyerImageLarge2.file = myServerImage
            self.flyerImageLarge2.loadInBackground()
        }
    if let myServerImage = object.valueForKey("ImageThree") as? PFFile {
            self.flyerImageLarge3.file = myServerImage
            self.flyerImageLarge3.loadInBackground()
        }
    }
}

我建议添加if let语句,以便在图像不存在时不会出错。

答案 1 :(得分:0)

如果我说的是显而易见的或已经考虑过,请原谅我。我并不完全熟悉你在做什么,但看起来很合适。

假设您尚未使用它,则需要使用recursive function。基本上,这是一个自我调用直到结束条件的函数。

我不熟悉您的代码,因此我将在JavaScript中使用一个简单的示例进行演示:

/* A "public" function that your program will call */
function getStuff(total)
{
    //quick positive check
    if (total > 0)
    {
        //start and pass in an empty array
        return _getStuffRecursion(total, []);
    }
    else
    {
        //total is not positive, return empty array
        return [];
    }
}

/* A "private" function that will do the recursion */
function _getStuffRecursion(total, resultsArray)
{
    //do work this is where you would call your function that does the work.
    var someResource = Math.random();

    //add work to the array collected so far
    resultsArray.push(someResource);

    //change count
    var newTotal = total - 1;

    //check condition
    if (newTotal > 0)
    {
        //recursive condition, go to the next level down and pass in what is collected so far
        return _getStuffRecursion(newTotal, resultsArray)
    }
    else
    {
        //end condition met, just return the array with everything collected from the upper levels
        return resultsArray;
    }
}

/* Start */

//get started by calling the "public" function
var results = getStuff(20);

//print it to console
console.log(results);

如果此解决方案有效,我确定您可以将其调整为Parse。