Swift 1.2,如何从Parse1.7中正确检索图像?

时间:2015-07-07 09:35:39

标签: ios image swift parse-platform tableview

// import UIKit
import Parse
import Foundation

class EditFriendsController: UITableViewController {

    var allUsers:NSArray = [] // NSArray *allUsers
    var currentUser = PFUser.currentUser()
    var friends:NSMutableArray = [] // used in FriendsController
    var profileImages = [PFFile]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // PFQuery *query = [PFUser query] in objective C
        var query = PFUser.query()
        query!.orderByAscending("username")
        query!.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in
            if error != nil {
                // Log details of the failure
                println("Error: \(error!)\(error!.userInfo)")
            } else {
                // The find succeeded.
                println("Successfully retrieved \(objects!.count) scores.")
                self.allUsers = objects!
                // println("\(self.allUsers)")
                self.animateTable() // include self.tableView.reloadData()
            }
        }
        self.currentUser = PFUser.currentUser()
    }

    // MARK: - Table view animate function
    func animateTable() {
        tableView.reloadData()

        let cells = tableView.visibleCells()
        let tableHeight:CGFloat = tableView.bounds.size.height

        for transformBefore in cells {
            let cell: UITableViewCell = transformBefore as! UITableViewCell
            cell.transform = CGAffineTransformMakeTranslation(0, tableHeight)
        }        
        var index = 0

        for transformAfter in cells {
            let cell: UITableViewCell = transformAfter as! UITableViewCell
            UIView.animateWithDuration(1.5, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: nil, animations: { () -> Void in
                cell.transform = CGAffineTransformMakeTranslation(0, 0)
            }, completion: nil)

            index += 1
        }
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {        
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.allUsers.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! EditFriendsProfileViewCell

        var user = self.allUsers[indexPath.row] as! PFUser
        cell.usernameLabel.text = user.username!

        // cell.profileImageView.image = UIImage(named: profileImages[indexPath.row])
        if let userPicture = PFUser.currentUser()?["photo"] as? PFFile {
            userPicture.getDataInBackgroundWithBlock({
                (imageData:NSData?, error:NSError?) -> Void in
                var img:UIImageView = UIImageView()
                if (error == nil) {
                    cell.profileImageView.image = UIImage(data: imageData!)    
                } else {

                }
            })
        }

    // image cornerRadius

        cell.profileImageView.layer.cornerRadius = 10
        cell.profileImageView.clipsToBounds = true

        var myFriend = isFriend(user)

        cell.accessoryType = myFriend ? .Checkmark : .None
        //cell.checkImageView.image = myFriend ? UIImage(named: "checkedFilled.png") : UIImage(named: "checkedWhite.png")
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        self.tableView.deselectRowAtIndexPath(indexPath, animated: false)
        var cell = tableView.cellForRowAtIndexPath(indexPath) as! EditFriendsProfileViewCell
        var user = self.allUsers[indexPath.row] as! PFUser
        var friendsRelation = self.currentUser!.relationForKey("friendsRelation")
        var myFriend = isFriend(user)
        if (myFriend) {
            // remove process
            // 1. Remove the checkmark
            cell.accessoryType = UITableViewCellAccessoryType.None
            // cell.imageView?.image = UIImage(named: "checkedWhite.png")
            // 2. Remove from array of friend
            var friend = PFUser()
            for friend in self.friends  {
                if friend.objectId == user.objectId {
                    self.friends.removeObject(friend)
                    break;
                }
            }
            // 3. Remove from the backend
            friendsRelation.removeObject(user)
        } else {
            // add them
            cell.accessoryType = UITableViewCellAccessoryType.Checkmark
            // cell.imageView?.image = UIImage(named: "checkedFilled.png")
            self.friends.addObject(user)
            friendsRelation.addObject(user)
        }
        self.currentUser!.saveInBackgroundWithBlock {
            (success: Bool, error: NSError?) -> Void in
            if (success) {
                // The post has been added to the user's likes relation.
            } else {
                // There was a problem, check error.description
                println("Error: \(error!)\(error!.userInfo)")
            }
        }
    }

    // MARK: - Table view Helper Methods
    func isFriend(user: PFUser!) -> Bool {
        var friend = PFUser()
        for friend in self.friends  {
            if friend.objectId == user.objectId {
                return true;
            }
        }
        return false;
    }
}

尽管所有不同的商店都存储在Parse中,但所有个人资料照片都会被加载为相同的图像。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

您正在设置当前登录用户的照片,设置用户?["照片"]在cellForRowAtIndexPath中

if let userPicture = user?["photo"] as? PFFile{

    }