Swift - 从解析接收图像并显示它们的问题
Posted
技术标签:
【中文标题】Swift - 从解析接收图像并显示它们的问题【英文标题】:Swift - Problems receiving images from parse and displaying them 【发布时间】:2015-12-13 16:29:08 【问题描述】:我在从解析中获取用户图像并将它们加载到表格视图时遇到问题。我认为这与 userId /Strings 和指针业务有关!
图片上传很好解析,我可以看到它们,但我似乎无法下载它们。我的代码绝对是问题所在,我只是不知道是哪一部分。
解析时,userId 是一个指向用户类的指针,所以我很确定我在连接它时做错了..!我是 swift & Parse 的新手,所以我仍然很难理解这一切。
下面是我的代码,如果有人理解这个问题,如果你能告诉我,我会很高兴的!!
import UIKit
import Parse
class TimelineTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPopoverPresentationControllerDelegate
@IBOutlet var feedTableView: UITableView!
var refresher:UIRefreshControl!
var titles = [String]()
var username = [String]()
var imageFiles = [PFFile]()
var users = [String: String]()
override func viewDidLoad()
super.viewDidLoad()
let query = PFUser.query()
query?.findObjectsInBackgroundWithBlock( (objects, error) -> Void in
if let users = objects
self.titles.removeAll(keepCapacity: true)
self.imageFiles.removeAll(keepCapacity: true)
self.users.removeAll(keepCapacity: true)
self.username.removeAll(keepCapacity: true)
for object in users
if let user = object as? PFUser
self.users[user.objectId!] = user.username!
let getFollowedUsersQuery = PFQuery(className: "followers")
getFollowedUsersQuery.whereKey("follower", equalTo: PFUser.currentUser()!)
getFollowedUsersQuery.findObjectsInBackgroundWithBlock (objects, error) -> Void in
if let objects = objects
for object in objects
let followedUser = object ["following"] as! String
let query = PFQuery(className: "Post")
query.whereKey("userId", equalTo: followedUser)
query.findObjectsInBackgroundWithBlock( (objects, error) -> Void in
if let objects = objects
for object in objects
self.titles.append(object["title"]! as! String)
self.imageFiles.append(object["imageFile"]! as! PFFile)
self.username.append(self.users[object["userId"] as! String]!)
self.tableView.reloadData()
) )
// Add the refresher
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "")
refresher.addTarget(self, action: "refreshData", forControlEvents: UIControlEvents.ValueChanged)
// Add the refresher as a sub view on the tableview
self.feedTableView.addSubview(refresher)
func refreshData() -> Void
self.refresher.endRefreshing()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
@IBAction func blogPost(sender: AnyObject)
@IBAction func CameraPopover(sender: AnyObject)
self.performSegueWithIdentifier("cameraPopover", sender: self)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if segue.identifier == "cameraPopover"
let vc = segue.destinationViewController
let controller = vc.popoverPresentationController
if controller != nil
controller?.delegate = self
vc.preferredContentSize = CGSizeMake(50, 90)
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle
return.None
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
// #warning Incomplete implementation, return the number of sections
return 1
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
// #warning Incomplete implementation, return the number of rows
return username.count
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let postCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TimelineTableViewCell
imageFiles[indexPath.row].getDataInBackgroundWithBlock (data, error) -> Void in
if let downloadedImage = UIImage(data: data!)
postCell.postedImage.image = downloadedImage
postCell.usernameLabel.text = username[indexPath.row]
postCell.titleLabel.text = titles[indexPath.row]
return postCell
【问题讨论】:
【参考方案1】:你提到的 userId 指针的问题在这里:
let followedUser = object ["following"] as! String
let query = PFQuery(className: "Post")
query.whereKey("userId", equalTo: followedUser)
您提到Post
类的userId
列是一个User
指针,但是您将它作为带有followedUser
的字符串引用。
关于无法正常显示的图片,在您刷新 UI 之前它们不会显示。您正在使用正确的代码来加载数据
imageFiles[indexPath.row].getDataInBackgroundWithBlock (data, error) -> Void in
if let downloadedImage = UIImage(data: data!)
postCell.postedImage.image = downloadedImage
但是,问题在于,由于数据是异步加载,postCell
将在数据被提取之前返回到表中。缺少的关键部分是在完成处理程序中使用self.tableView.reloadData()
刷新表。
imageFiles[indexPath.row].getDataInBackgroundWithBlock (data, error) -> Void in
if let downloadedImage = UIImage(data: data!)
postCell.postedImage.image = downloadedImage
self.tableView.reloadData()
或者,我建议考虑使用来自ParseUI
框架的PFImageView
。在完成查询时将每个PFFiles
分配给PFImageView
,然后在每个PFImageView
上调用loadInBackground
。一旦数据加载完毕,PFImageView
类会自动处理图像更新。
【讨论】:
嗯,我添加了 reloadData。但仍然没有,我不确定它是否连接到解析,可能是因为你上面提到的字符串业务? 如果指针/字符串的东西没有先固定,它将无法工作。PFFiles
作为Post
查询的一部分返回。只要存在“指针等于字符串”的无效约束,该查询就不会返回任何对象,这永远不会发生。以上是关于Swift - 从解析接收图像并显示它们的问题的主要内容,如果未能解决你的问题,请参考以下文章
Swift:从 Internet 下载图像并缓存它们无法正常工作。需要建议