图像未出现在 PFQueryCollectionViewController 中 [英] Images not appearing in PFQueryCollectionViewController

查看:22
本文介绍了图像未出现在 PFQueryCollectionViewController 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 iPhone 应用程序,它应该在 UICollectionView 中显示图像.图像保存在 Parse 云中,我使用 PFQueryCollectionViewController 的子类进行查询并显示图像.

点击MKMapView 标注会触发显示CollectionViewController 的segue.第一次显示 CollectionViewController 时,图像不会出现在单元格中.但是,如果我返回到 MapView,然后返回到 CollectionViewController,图像会出现在单元格中.

如何在第一次显示 PFQueryCollectionViewController 时显示图像?

这是我的代码:

class PhotoGridCollectionViewController: PFQueryCollectionViewController {覆盖 func viewDidAppear(动画:布尔){}覆盖 func viewDidLoad() {super.viewDidLoad()}覆盖 func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()//处理任何可以重新创建的资源.}//MARK: UICollectionViewDataSource覆盖 func numberOfSectionsInCollectionView(collectionView: UICollectionView) ->整数{//#warning 方法实现不完整——返回节数返回 1}覆盖 func collectionView(collectionView: UICollectionView, numberOfItemsInSection 部分: Int) ->整数{//#warning Incomplete 方法实现 -- 返回部分中的项目数返回 self.objects.count}覆盖 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath, object: PFObject?) ->PFCollectionViewCell?{let cell = collectionView.dequeueReusableCellWithReuseIdentifier("venuePhotoThumb", forIndexPath: indexPath) as!PhotoGridCollectionViewCell如果让 pfObject = 对象 {cell.imageView.file = pfObject["imageFile"] 作为?PF文件cell.imageView.loadInBackground({ (img, err) -> Void inprintln("下载完成")})}返回单元格}}

因为我使用的是 Storyboards,所以我通过在属性检查器中设置它来传递我的自定义类 parseClass:

如你所见,我使用PFImageViewloadInBackground方法异步加载图像,我认为这个问题可能是由图像引起的在单元返回后被下载,但我没有任何其他想法.有谁知道为什么第一次显示视图时图像没有出现?

解决方案

我的合作开发者终于找到了答案.在 cellForItemAtIndexPath 中配置单元格时,我们必须为 PFImageViews 设置一个占位符图像.Parse 上的代码示例确实包括设置占位符图像,但它们并没有说 PFQueryCollectionViewController 需要它才能正常工作.我们认为这是一种美感,我们可以稍后再使用.

所以答案是:为了在 PFQueryCollectionViewController 的单元格中正确加载图像,您必须在 cellForItemAtIndexPath 中配置单元格时提供占位符图像.

这是有效的代码:

let placeholder = UIImage(named: "myPlaceholderImage")//一个来自你xcode项目中images.xcassets的图片cell.imageView.image = 占位符如果让 pfObject = 对象 {cell.imageView.file = pfObject["image"] 作为?PF文件cell.imageView.loadInBackground()}

I'm working on an iPhone app that should display images in a UICollectionView. The images are saved on the Parse cloud, and I'm using a subclass of PFQueryCollectionViewController to make the query and display the images.

Tapping on a MKMapView callout triggers the segue that shows the CollectionViewController. The images do not appear in the cells the first time the CollectionViewController is presented. However, if I go back to the MapView, and then return to the CollectionViewController, the images appear in the cells.

How can I get the images to appear the first time the PFQueryCollectionViewController is presented?

Here's my code:

class PhotoGridCollectionViewController: PFQueryCollectionViewController {

override func viewDidAppear(animated: Bool) {
}

override func viewDidLoad() {
  super.viewDidLoad()
}

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

// MARK: UICollectionViewDataSource

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
  //#warning Incomplete method implementation -- Return the number of sections
  return 1
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  //#warning Incomplete method implementation -- Return the number of items in the section
  return self.objects.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFCollectionViewCell? {

  let cell = collectionView.dequeueReusableCellWithReuseIdentifier("venuePhotoThumb", forIndexPath: indexPath) as! PhotoGridCollectionViewCell

  if let pfObject = object {
    cell.imageView.file = pfObject["imageFile"] as? PFFile
    cell.imageView.loadInBackground({ (img, err) -> Void in
      println("Download complete")
    })
  }

  return cell
}

}

Since I'm using Storyboards, I pass my custom class the parseClass by setting it in the Attribute Inspector:

As you can see, I'm using the loadInBackground method of the PFImageView to load the images asynchronously, and I think this the problem might be caused by the images being downloaded after the cell is returned, but I don't have any other ideas. Does anyone know why the images are not appearing the first time the view is presented?

解决方案

My co-developer finally found the answer to this. We had to set a placeholder image for the PFImageViews when configuring the cells in cellForItemAtIndexPath. The code examples on Parse do include setting the placeholder image, but they don't say that it is required for the PFQueryCollectionViewController to work properly. We considered it an aesthetic touch that we could come back to later.

So the answer is: in order for images to load properly in the cells of a PFQueryCollectionViewController you MUST provide a placeholder image when configuring the cells in cellForItemAtIndexPath.

Here's the code that worked:

let placeholder = UIImage(named: "myPlaceholderImage")  //an image from images.xcassets in your xcode project
cell.imageView.image = placeholder

if let pfObject = object {
    cell.imageView.file = pfObject["image"] as? PFFile
    cell.imageView.loadInBackground()
}

这篇关于图像未出现在 PFQueryCollectionViewController 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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