基本上我想做的是从 Parse 获取图像并将其存储在变量中,然后我可以将其设置为 tableViewCell 中的 ImageView 。
从 Parse 获取图像后,我不知道如何存储图像。
这是我的代码:
import UIKit
import Parse
import Bolts
class YourEvents: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var currentuser = PFUser.currentUser()?.username
//array
var testArray = [String]()
var testdecr = [String]()
var imagestored = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
var query = PFQuery(className:"Companies")
let pUserName = PFUser.currentUser()?["username"] as? String
query.whereKey("createdby", equalTo:"\(pUserName)")
// let runkey = query.orderByAscending("companyname")
query.findObjectsInBackgroundWithBlock{
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
//do something with the found objects
if let objects = objects as [PFObject]! {
for object in objects {
let load = object.objectForKey("companyname") as! String
self.testArray .append(load)
print(self.testArray)
let load2 = object.objectForKey("companydescription") as! String
self.testdecr.append(load2)
print(self.testdecr)
//let images: PFFile = object["imagefile"] as! PFFile
if let userPicture = ["picture"] as? PFFile {
userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
self.imagestored.image = UIImage(data:imageData)
}
}
}
}
}
} else {
//log details of failure
print("Error: \(error!) \(error?.userInfo) ")
}
}
// reload UIViewController and UITabkeView
sleep(3)
do_table_refresh()
}
func do_table_refresh () {
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
return
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return testArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("yourstartups", forIndexPath: indexPath) as! YourStartupsCell
cell.lbTitle!.text = self.testArray[indexPath.row]
cell.lbDescription!.text = self.testdecr[indexPath.row]
return cell
}
}
最佳答案
使用 PFImageView 而不是 UIImageView 可以让处理 Parse 中的文件变得不那么麻烦。 Parse 将处理缓存图像以加快检索速度,并为您提供设置占位符图像或在检索图像后执行任何工作的方法。
这是一个 imageView 类型为 PFImageView 的示例。只是不要忘记导入 ParseUI。
let file = object["picture"] as? PFFile;
imageView.file = file
imageView.image = .... //set placeholder image
imageView.loadInBackground(nil) //will asynchronously download the image if not cached
关于ios - 存储来自 Parse 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35018859/