使用swift中的parse填充图像数组 [英] populate image array with parse in swift

查看:168
本文介绍了使用swift中的parse填充图像数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我是swift和parse.com的新手,我正在尝试使用已解压缩的图像填充我的数组,尝试使用dispatch_async但不知道它是如何工作的,继承人代码:

Hi I'm new with swift and parse.com, I'm trying do populate my array with already saved images in parse, try to use the dispatch_async but don't know how this works, heres the code:

//imageArray declaration in table:
var imageArray: Array<UIImage> = []


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell

    var query = PFQuery(className: "ParseClass")

    query.findObjectsInBackgroundWithBlock { ( objects: [AnyObject]?, error: NSError?) -> Void in

        if(error == nil) {

            let imageObjects = objects as! [PFObject]

            for object in objects! {

                let thumbNail = object["columnInParse"] as! PFFile

                thumbNail.getDataInBackgroundWithBlock ({ (imageData: NSData?, error: NSError?) -> Void in

                    if (error == nil) {

                        if let image = UIImage(data:imageData!) {

 //here where the error appears: Cannot invoke 'dispatch_async' with an argument list of type '(dispatch_queue_t!, () -> _)' 
                           dispatch_async(dispatch_get_main_queue()) {                                     
                                cell.image.image = self.imageArray.append( image )
                            }

                        }

                    }
                })
            }
        }
        else{

            println("Error in retrieving \(error)")

        }
    }

    return cell
}

希望大家都能理解这段代码。

Hope you people understand that code.

推荐答案

使用该调度块的正确方法如下:

The proper way to use that dispatch block is like so:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
                //perform your work here
                self.imageArray.append(image)
                cell.image.image = imageArray[indexPath.row]
            })

编译器发出此警告的另一个原因是您尝试将图像附加到数组将其设置为单元格图像。您应该做的事情如上所示。

Another reason that the compiler is giving this warning is because you're trying to append images to an array and set that as the cells image. What you should be doing is shown above.

最后,我建议您将查询从 cellForRowAtIndexPath:中移出分成单独的方法因为这是糟糕的代码设计。

Finally, I would recommend moving your queries out of cellForRowAtIndexPath: and into seperate methods as this is bad code design.

编辑:重写方法以提供良好代码设计的示例......

Rewrote method to provide examples of good code design...

var imageArray = [UIImage]()

func performLookupQuery() {
    var query = PFQuery(className: "ParseClass")
    query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?)
        if error == nil {
            let imageObjects = objects as! [PFObject]
            for object in imageObjects {
                let thumbnail = object["columnInParse"] as! PFFile
                thumbnail.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?)
                    if error == nil {
                        if let image = UIImage(data: imageData!) {
                            imageArray.append(image)
                            //now your imageArray has all the images in it that you're trying to display
                            //you may need to reload the TableView after this to get it to display the images

                        }
                    }
                }
            }
        }
        self.tableView.reloadData()
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    cell.image.image = nil
    cell.image.image = imageArray[indexPath.row]
    return cell
}

我没有在查询中添加所有错误检查像你应该的方法,我只是写它来表明你会做什么。现在您已经有了这个查询方法,可以在 viewDidLoad 中调用它,然后当您尝试加载表时结果将可用。

I didn't add all the error checking in the query method like you should, I was just writing it to show what you would do. Now that you have this query method, you can call it in viewDidLoad and then the results will be available when you try to load your table.

override func viewDidLoad(animated: Bool) {
    super.viewDidLoad(animated)
    self.performLookupQuery()
}

这篇关于使用swift中的parse填充图像数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆