从Parse获取随机图像

时间:2017-09-12 21:53:13

标签: swift image random pfquery

我在堆栈溢出中尝试了很多代码,但几乎所有代码都是针对非Parse的核心数据。

我尝试从kombin.swift的Parse获取随机图像。 然后使用按钮随机更改图像。 我应该使用哪个代码随机创建图像,然后随机更改图像(使用按钮)?

这是我的ViewController:

import UIKit
import Parse

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    var placeNameArray = [String]()
    var chosenPlace = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        getData()
    }

    func getData() {
        let query = PFQuery(className: "Places")
        query.findObjectsInBackground  { (objects, error) in
            if error != nil {
                let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
                let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
                alert.addAction(okButton)
                self.present(alert, animated: true, completion:nil)
            } else {
                self.placeNameArray.removeAll(keepingCapacity: false)
                for object in objects! {
                    self.placeNameArray.append(object.object(forKey: "name") as! String)
                }
                self.tableView.reloadData()
            }
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "1to4"
        {
            let destinationVC = segue.destination as! ViewController4
            destinationVC.selectedPlace = self.chosenPlace
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        self.chosenPlace = placeNameArray[indexPath.row]
        self.performSegue(withIdentifier: "1to4", sender: nil)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = placeNameArray[indexPath.row]
        return cell
    }

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

    @IBAction func ekle(_ sender: Any) {
        performSegue(withIdentifier: "1to2", sender: nil)
    }
}

我的kombin.swift:

import UIKit
import Parse

class kombin: UIViewController {
    @IBOutlet weak var imageView: UIImageView!

    var imageArray = [PFFile]()
    var placeNameArray = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        func randomImage() -> UIImage {
            let unsignedArrayCount = UInt32(imageArray.count)
            let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
            let randomNumber = Int(unsignedRandomNumber)
            return UIImage(named: placeNameArray[randomNumber])!
        }
    }

0 个答案:

没有答案