ios - 从 Parse 中检索图像

标签 ios swift parse-platform

我正在尝试从 Parse 获取图像。我一直在研究这段代码:

import UIKit
import Parse
import ParseUI

class ViewControllerHome: UIViewController, UITableViewDelegate, UITableViewDataSource {

var imagesFiles = [PFFile]()
var imageUser = [String]()
var imageTitle = [String]()

@IBOutlet weak var MessageTable: UITableView!

let color = UIColor(red: 0.0/255.0, green: 105.0/255.0, blue: 92.0/255.0, alpha: 1)
let colore = UIColor.whiteColor()
let coloree = UIColor(red: 33.0/255.0, green: 33.0/255.0, blue: 33.0/255.0, alpha: 1)

override func viewDidLoad() {
    
    let query = PFQuery(className: "Messages")
    query.orderByDescending("createdAt")
    query.findObjectsInBackgroundWithBlock { (posts: [PFObject]?, error: NSError?) -> Void in
        
        if error == nil {
            
            for post in posts! {
                
                self.imagesFiles.append(post["Post"] as! PFFile)
                self.imageUser.append(post["Name"] as! String)
                self.imageTitle.append(post["Title"] as! String)
                
            }
            
            self.MessageTable.reloadData()
            
            
        }else{
            
            print(error)
            
        }
        
    }
    
    super.viewDidLoad()
    
    self.navigationController?.navigationBar.barTintColor = color
    self.navigationController?.navigationBar.tintColor = colore
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: colore]
    UITabBar.appearance().barTintColor = coloree
    UITabBar.appearance().tintColor = colore
    UITabBar.appearance().translucent = false
    
    self.MessageTable.delegate = self
    self.MessageTable.dataSource = self

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = MessageTable.dequeueReusableCellWithIdentifier("cell")! as! TableViewCellHome
    
    //text
    cell.UsernameLabel.text = imageUser[indexPath.row]
    cell.Title.text = imageTitle [indexPath.row]
    
    //image
    imagesFiles[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in

        if imageData != nil {
            
            let image = UIImage(data: imageData!)
            cell.PostImage.image = image
            
        }else{
            
            print(error)
            
        }
        
    }
    
    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    return imagesFiles.count
}

@IBAction func refresh(sender: AnyObject) {
    
    MessageTable.reloadData()
}
}

当我运行它时,它只返回用户名和帖子标题,但图像应该所在的位置是空白的。这是日志:

提前致谢

最佳答案

从这些细节:

(lldb) po cell.PostImage 
<UITableViewCellContentView: 0x7fb700ebdc00; frame = (0 0; 320 389); opaque = NO; gestureRecognizers = <NSArray: 0x7fb700ea4c10>; layer = <CALayer: 0x7fb700e71d20>> 

(lldb) po cell 
<Talent.TableViewCellHome: 0x7fb700ebd3f0; baseClass = UITableViewCell; frame = (0 0; 320 389); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x7fb700e7c7b0>>

我们可以看到您出队的单元格是好的,但是对 ImageView 的引用实际上是引用单元格内容 View 。

这可能是导出连接错误,您只需删除现有连接并重新连接到 ImageView 即可。

关于ios - 从 Parse 中检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34970465/

相关文章:

javascript - 解析 Facebook 登录错误 : "You must initialize FacebookUtils before calling logIn"

ios - 不合理的堆增长

ios - 动画 UIButton 状态淡入淡出点击

objective-c - 带指针的属性与不带指针的属性之间的区别 objective-c

ios - 协议(protocol)委托(delegate)始终为零

java - 推送通知条件

php - 移动/Web 应用程序后端

iOS - TapGestureRecognizer - 点击适用于整个屏幕而不是 View

ios - 从 map View 上长按触发推送 segue

swift - 从 Firebase 检索数据不起作用