如何在 Parse 中获取图像,单元格名称有效但图像不加载
How get images in Parse, Cell name works but images don't load
我可以从 parse 加载图片,但是它们有代号,我想在单元格中显示图片,请不要加载图片!
//
import UIKit
import Parse
import Bolts
class LojasViewController: UIViewController, UITableViewDelegate {
var ParseData = [PFObject]()
var ParseImages = [PFFile]()
@IBOutlet weak var saloesTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Retrieving Parse Information
var query = PFQuery(className:"Saloes")
query.orderByAscending("nome")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println(objects!.count)
// Do something with the found objects
if let objects = objects as? [PFObject] {
self.ParseData = objects
println(self.ParseData.count)
self.saloesTableView.reloadData()
for object in objects {
println(object["nome"])
println(object["cidade"])
println(object["imagemCelula"])
println(object["endereco"])
println(object["GeoPonto"])
println(object["telefone"])
println(object["celular"])
println(object["texto"])
println(object["Imagem1"])
println(object["Imagem2"])
println(object["Imagem3"])
println(object["Imagem4"])
println(object["Imagem5"])
println(object["Imagem6"])
println(object["Imagem7"])
println(object["Imagem8"])
println(object["Imagem9"])
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Esconde statusBar
override func prefersStatusBarHidden() -> Bool {
return true
}
@IBAction func voltarButton(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func loadData () {
}
// MARK: - Table view data source ____________________________________
func numberOfSectionsInTableView(tableView: UITableView!) -> Int
{
return 1
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
return ParseData.count
}
func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?
{
let cell:SaloesTableViewCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as! SaloesTableViewCell
cell.nomeSalao.text = ParseData[indexPath!.row]["nome"] as? String
let imagemCelula = ParseData[indexPath!.row]["imagemCelula"] as! PFFile
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
}
}
您可以使用 findObjectsInBackgroundWithBlock
获取 Parse 对象。但是一旦你有了图像的解析对象,你需要通过调用 getDataInBackgroundWithBlock
.
下载实际文件
这是您的代码:
我更新了 ParseImages,它现在是一个 NSData 数组。
我实现了getDataInBackgroundWithBlock
。
我从您从 Parse 检索到的图像中为 imagemCelula
分配了一个 UIImage。
让我知道它是如何工作的
import UIKit
import Parse
import UIKit
import Parse
import Bolts
class LojasViewController: UIViewController, UITableViewDelegate {
var ParseData = [PFObject]()
var ParseImages: [NSData] = []
@IBOutlet weak var saloesTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Retrieving Parse Information
var query = PFQuery(className:"Saloes")
query.orderByAscending("nome")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println(objects!.count)
// Do something with the found objects
if let objects = objects as? [PFObject] {
self.ParseData = objects
println(self.ParseData.count)
self.saloesTableView.reloadData()
for object in objects {
object.getDataInBackgroundWithBlock({ (parseImageData, error) -> Void in
if (error == nil) {
let thatImageData: NSData = parseImageData
ParseImages.append(thatImageData)
println(object["nome"])
println(object["cidade"])
println(object["imagemCelula"])
println(object["endereco"])
println(object["GeoPonto"])
println(object["telefone"])
println(object["celular"])
println(object["texto"])
println(object["Imagem1"])
println(object["Imagem2"])
println(object["Imagem3"])
println(object["Imagem4"])
println(object["Imagem5"])
println(object["Imagem6"])
println(object["Imagem7"])
println(object["Imagem8"])
println(object["Imagem9"])
} else {
println("failed to load images")
}
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Esconde statusBar
override func prefersStatusBarHidden() -> Bool {
return true
}
@IBAction func voltarButton(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func loadData () {
}
// MARK: - Table view data source ____________________________________
func numberOfSectionsInTableView(tableView: UITableView!) -> Int
{
return 1
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
return ParseData.count
}
func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?
{
let cell:SaloesTableViewCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as! SaloesTableViewCell
cell.nomeSalao.text = ParseData[indexPath!.row]["nome"] as? String
let imagemCelula = UIImage(data: ParseImages[indexPath!.row])
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
}
}
我可以从 parse 加载图片,但是它们有代号,我想在单元格中显示图片,请不要加载图片!
//
import UIKit
import Parse
import Bolts
class LojasViewController: UIViewController, UITableViewDelegate {
var ParseData = [PFObject]()
var ParseImages = [PFFile]()
@IBOutlet weak var saloesTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Retrieving Parse Information
var query = PFQuery(className:"Saloes")
query.orderByAscending("nome")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println(objects!.count)
// Do something with the found objects
if let objects = objects as? [PFObject] {
self.ParseData = objects
println(self.ParseData.count)
self.saloesTableView.reloadData()
for object in objects {
println(object["nome"])
println(object["cidade"])
println(object["imagemCelula"])
println(object["endereco"])
println(object["GeoPonto"])
println(object["telefone"])
println(object["celular"])
println(object["texto"])
println(object["Imagem1"])
println(object["Imagem2"])
println(object["Imagem3"])
println(object["Imagem4"])
println(object["Imagem5"])
println(object["Imagem6"])
println(object["Imagem7"])
println(object["Imagem8"])
println(object["Imagem9"])
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Esconde statusBar
override func prefersStatusBarHidden() -> Bool {
return true
}
@IBAction func voltarButton(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func loadData () {
}
// MARK: - Table view data source ____________________________________
func numberOfSectionsInTableView(tableView: UITableView!) -> Int
{
return 1
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
return ParseData.count
}
func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?
{
let cell:SaloesTableViewCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as! SaloesTableViewCell
cell.nomeSalao.text = ParseData[indexPath!.row]["nome"] as? String
let imagemCelula = ParseData[indexPath!.row]["imagemCelula"] as! PFFile
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
}
}
您可以使用 findObjectsInBackgroundWithBlock
获取 Parse 对象。但是一旦你有了图像的解析对象,你需要通过调用 getDataInBackgroundWithBlock
.
这是您的代码:
我更新了 ParseImages,它现在是一个 NSData 数组。
我实现了getDataInBackgroundWithBlock
。
我从您从 Parse 检索到的图像中为 imagemCelula
分配了一个 UIImage。
让我知道它是如何工作的
import UIKit
import Parse
import UIKit
import Parse
import Bolts
class LojasViewController: UIViewController, UITableViewDelegate {
var ParseData = [PFObject]()
var ParseImages: [NSData] = []
@IBOutlet weak var saloesTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Retrieving Parse Information
var query = PFQuery(className:"Saloes")
query.orderByAscending("nome")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println(objects!.count)
// Do something with the found objects
if let objects = objects as? [PFObject] {
self.ParseData = objects
println(self.ParseData.count)
self.saloesTableView.reloadData()
for object in objects {
object.getDataInBackgroundWithBlock({ (parseImageData, error) -> Void in
if (error == nil) {
let thatImageData: NSData = parseImageData
ParseImages.append(thatImageData)
println(object["nome"])
println(object["cidade"])
println(object["imagemCelula"])
println(object["endereco"])
println(object["GeoPonto"])
println(object["telefone"])
println(object["celular"])
println(object["texto"])
println(object["Imagem1"])
println(object["Imagem2"])
println(object["Imagem3"])
println(object["Imagem4"])
println(object["Imagem5"])
println(object["Imagem6"])
println(object["Imagem7"])
println(object["Imagem8"])
println(object["Imagem9"])
} else {
println("failed to load images")
}
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Esconde statusBar
override func prefersStatusBarHidden() -> Bool {
return true
}
@IBAction func voltarButton(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func loadData () {
}
// MARK: - Table view data source ____________________________________
func numberOfSectionsInTableView(tableView: UITableView!) -> Int
{
return 1
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
return ParseData.count
}
func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?
{
let cell:SaloesTableViewCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as! SaloesTableViewCell
cell.nomeSalao.text = ParseData[indexPath!.row]["nome"] as? String
let imagemCelula = UIImage(data: ParseImages[indexPath!.row])
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
}
}