无法分配UIImage类型的值?输入PFFile?

时间:2016-07-16 18:54:46

标签: ios swift parse-platform uiimage

我是编程和遇到从Parse获取图像并将其放入图像视图时遇到问题的新手。我试图调用'getDataInBackgroundWithBlock',但自动完成不会显示它。

class ListOfCategoriesViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

var categories: [DisplayCategory] = []


override func viewDidLoad() {

    super.viewDidLoad()

    // let queryCategories = DisplayCategory.query()

    let titleQuery = PFQuery(className: "Category")
    titleQuery.whereKeyExists("title")
    let imageQuery = PFQuery(className: "Category")
    imageQuery.whereKeyExists("imageCategory")

    let query = PFQuery.orQueryWithSubqueries ([titleQuery, imageQuery])

    query.findObjectsInBackgroundWithBlock {(result: [PFObject]?, error: NSError?) -> Void in

        self.categories = result as? [DisplayCategory] ?? []

        for category in self.categories {
            do {
                let data = try category.imageCategory?.getData()

                category.imageCategory = UIImage(data: data!, scale:1.0) // compiler error here
            } catch {
                print("could not get image")
            }
        }
    self.tableView.reloadData()
    }
  }


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

extension ListOfCategoriesViewController: UITableViewDataSource {

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return categories.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCellWithIdentifier("CategoryCell") as! CategoryTableViewCell

    cell.categoryImageView.image = categories[indexPath.row].imageCategory //compiler error here

    return cell
   }
}

DisplayCategory类看起来像这样。我的想法是显示一个带有来自Parse的文本和图像的表视图单元格,所以我认为这是创建新类的最佳选择,而不是字典。

import Foundation
import Parse

class DisplayCategory: PFObject, PFSubclassing {


@NSManaged var imageCategory: PFFile?
@NSManaged var title: String?

static func parseClassName() -> String {
    return "Category"
}


override init () {
    super.init()
}

override class func initialize() {
    var onceToken : dispatch_once_t = 0;
    dispatch_once(&onceToken) {

        self.registerSubclass()
    }
}

}

2 个答案:

答案 0 :(得分:0)

DisplayCategory类/对象是什么样的?可能需要看到这一点。 同时你可以尝试单独查询图像,如此

let imageQuery = PFQuery(className:"Category")
let image = imageQuery.objectForKey("imageFile")! as? PFFile {
        image.getDataInBackgroundWithBlock({
            (imageData, error) -> Void in
            if (error == nil) {

//your for loop for setting the UIImage in DisplayCategory class/object here

            category.imageCategory = UIImage(data:imageData!)!

            }
        })
    }

查询您的"类别"解析类并检索" imageFile"中的项目。柱

答案 1 :(得分:0)

问题已解决。使代码更清晰,发现无需单独获取标题和图像。添加了var imageCategory:UIImage?到DisplayCategory类。

import Foundation
import UIKit
import Parse


class ListOfCategoriesViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

var categories: [DisplayCategory] = []


override func viewDidLoad() {

    super.viewDidLoad()

    let query = PFQuery(className: "Category")

    query.findObjectsInBackgroundWithBlock { (result: [PFObject]?, error: NSError?) -> Void in

        self.categories = result as? [DisplayCategory] ?? []
        print("received \(self.categories.count) categories from parse DB, now fetch individual images")

        for category in self.categories {
            print("fetch image for category: \(category.title)")

            category.imageCategoryFile?.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
                let image = UIImage(data: imageData!, scale: 1.0)
                category.imageCategory = image

                print("received image for category: \(category.title)")
                self.tableView.reloadData()
            }
        }
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

extension ListOfCategoriesViewController: UITableViewDataSource {

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return categories.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    print("create cell for row: \(indexPath.row)")

    let cell = tableView.dequeueReusableCellWithIdentifier("CategoryCell") as! CategoryTableViewCell

    cell.categoryImageView.image = categories[indexPath.row].imageCategory

    return cell
}

}

DisplayCategory类现在看起来像这样

import Foundation
import Parse

class DisplayCategory: PFObject, PFSubclassing {


@NSManaged var imageCategoryFile: PFFile?
@NSManaged var title: String?
var imageCategory: UIImage?

static func parseClassName() -> String {
    return "Category"
}


override init () {
    super.init()
}

override class func initialize() {
    var onceToken : dispatch_once_t = 0;
    dispatch_once(&onceToken) {
        // inform Parse about this subclass
        self.registerSubclass()
    }
}

}