从Parse存储图像

时间:2016-01-26 16:31:23

标签: ios swift parse-platform

基本上我尝试做的是从Parse获取图像并将其存储在变量中,然后我可以将其设置为tableViewCell中的图像视图。

一旦我从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
    }

}

my code

3 个答案:

答案 0 :(得分:0)

使用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

答案 1 :(得分:0)

您已将对象视为UIImage

var imagestored = UIImage()

然后您以UIImageView

的身份访问该对象
self.imagestored.image = UIImage(data:imageData)

这就是为什么你会收到这个错误。直接将图像存储到那个变量

self.imagestored = UIImage(data:imageData)

这将解决您的问题。

答案 2 :(得分:0)

错误在那里:

while (guess != computer)
{
    guess = (end - start) / 2 + start;
    if (guess != computer)
    {
        if (guess > computer)
        {
            end = guess;
            Console.WriteLine("Your guess is too high, next guess: {0}", guess);
        }
        else
        {
            start = guess;
            Console.WriteLine("Your guess is too low, guess again: {0}", guess);
        }
        count = count + 1;
        Console.WriteLine("Count {0}", count);
    }
}

尝试将其替换为:

self.imagestored.image = UIImage(data:imageData)