将图像从解析加载到集合视图 Swift

Load Image from parse to Collection View Swift

我想获得一些解决问题的建议。我想将存储在 Parse Server 上的图像加载到我的集合视图中,我正在使用以下代码:

import UIKit
import Parse

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    //var my_array:[String] = []
    //var my_array: [String] = [String]()
    var imageResources : Array<UIImage> = []
    //var query = PFQuery(className: "User")
    var query2 = PFQuery(className: "User")

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        //my_array = ["montagne.jpeg","montagne.jpeg","montagne.jpeg","montagne.jpeg","montagne.jpeg","montagne.jpeg","montagne.jpeg","montagne.jpeg","montagne.jpeg","montagne.jpeg","montagne.jpeg","montagne.jpeg","montagne.jpeg","montagne.jpeg"]

        query2.findObjectsInBackground{ (objects: [PFObject]?, error: Error?) -> Void in
            if error == nil {
                print("OK")

                for object in objects! {
                    print("IN FOR")
                    let thumbNail = object["image"] as! PFFile
                    thumbNail.getDataInBackground({
                        (imageData: Data?, error: Error?) -> Void in
                        if (error == nil) {
                            print("IN SECOND IF")
                            let image = UIImage(data:imageData!)
                            self.imageResources.append(image!)
                            //let size = self.imageResources.count
                            //print(size)
                        }
                    })
                }
            }
        }

        //query.findObjectsInBackground{ (objects: [PFObject]?, error: Error?) -> Void in
          //  if error == nil {
          //          for object in objects! {
          //              self.my_array.append(object.object(forKey: "image") as! String)

                  //  }
           // }
        }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageResources.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
        cell.ImageView?.image = imageResources[indexPath.row]
        return cell

    }
}

当我 运行 我的应用程序时,我没有任何错误,但是我没有在我的应用程序的集合视图中获取图像。图片在我的解析服务器中:Parser Server Interface

我刚刚发现在这段代码中,print print("IN FOR") 没有打印出来,所以我认为这里有问题,这就是为什么我的应用程序没有加载任何东西的原因:

query2.findObjectsInBackground{ (objects: [PFObject]?, error: Error?) -> Void in
            if error == nil {
                print("OK")

                for object in objects! {
                    print("IN FOR")
                    let thumbNail = object["image"] as! PFFile
                    thumbNail.getDataInBackground({
                        (imageData: Data?, error: Error?) -> Void in
                        if (error == nil) {
                            print("IN SECOND IF")
                            let image = UIImage(data:imageData!)
                            self.imageResources.append(image!)
                            //let size = self.imageResources.count
                            //print(size)
                        }
                    })
                }

但是当我在我的程序中使用修复图像并将其存储在一个数组中(注释中的代码)时,我可以在我的集合视图中看到这些图像...... 有人可以帮我解决这个问题吗?百万感谢。

乔迪。

User class 是 Parse 的内置 class,内部命名为 _User(带下划线),因此您的查询应如下所示:

var query2 = PFQuery(className: "_User")

或者,更紧凑:

var query2 = PFUser.query()

编辑

获取用户及其图像后,您必须重新加载集合视图以使其了解新数据;由于发生这种情况时您在后台队列中,因此您必须分派到主队列才能这样做:

for object in objects! {
  // ...
}
DispatchQueue.main.async {
  self.collectionView.reloadData()
}    

但是,我在你的代码中没有看到 UICollectionView;您只实现委托和数据源协议。你最好只用 subclass UICollectionViewController 而不是 UIViewController.