问题描述:

I am trying to retrieve user profile image from parse. I have a collection view and I am retrieving all images people posted. I want to show each users profile image in the cell as well. I was using the below code

override func viewDidLoad() {

super.viewDidLoad()

 let query = PFQuery(className: "Posts")

query.includeKey("pointName")

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

if error == nil

{

if let allQuestion = question as? [PFObject]

{

self.votes = allQuestion

self.collectionView.reloadData()

}

}

}

// Wire up search bar delegate so that we can react to button selections

// Resize size of collection view items in grid so that we achieve 3 boxes across

loadCollectionViewData()

}

/*

==========================================================================================

Ensure data within the collection view is updated when ever it is displayed

==========================================================================================

*/

// Load data into the collectionView when the view appears

override func viewDidAppear(animated: Bool) {

loadCollectionViewData()

}

/*

==========================================================================================

Fetch data from the Parse platform

==========================================================================================

*/

func loadCollectionViewData() {

// Build a parse query object

}

/*

==========================================================================================

UICollectionView protocol required methods

==========================================================================================

*/

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

return 1

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return self.votes.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("newview", forIndexPath: indexPath) as! NewCollectionViewCell

let item = self.votes[indexPath.row]

// Display "initial" flag image

var initialThumbnail = UIImage(named: "question")

cell.postsImageView.image = initialThumbnail

if let pointer = item["uploader"] as? PFObject {

cell.userName!.text = item["username"] as? String

print("username")

}

if let profile = item["uploader"] as? PFObject,

profileImageFile = profile["profilePicture"] as? PFFile {

cell.profileImageView.file = profileImageFile

cell.profileImageView.loadInBackground { image, error in

if error == nil {

cell.profileImageView.image = image

}

}

}

if let votesValue = item["votes"] as? Int

{

cell.votesLabel?.text = "\(votesValue)"

}

// Fetch final flag image - if it exists

if let value = item["imageFile"] as? PFFile {

println("Value \(value)")

cell.postsImageView.file = value

cell.postsImageView.loadInBackground({ (image: UIImage?, error: NSError?) -> Void in

if error != nil {

cell.postsImageView.image = image

}

})

}

return cell

}

However I found out that it sets profile image to the current user and not the user who posted the image. How can I do this? Thank you

UPDATE

so In parse my post class is

so I know who uploaded it but I don't know how to retrieve the profile image for this specific user.

网友答案:

You need to use a pointer that will point to the user who created the object. The profile photo should be in the user class. You then include the pointer in your query and that will return the user data.

网友答案:

okay, this might help you in objective-C

PFUser *user = PFUser *user = [PFUser currentUser]; 
[user fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
 _profileImage.file = [object objectForKey:@"profilePicture"]; 
}];
相关阅读:
Top