检索个人资料图片

时间:2015-08-04 06:08:30

标签: ios parse-platform

我正在尝试从解析中检索用户个人资料图片。我有一个集合视图,我正在检索人们发布的所有图像。我想在单元格中显示每个用户个人资料图像。我使用下面的代码     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
}

但是我发现它将配置文件图像设置为当前用户而不是发布图像的用户。我怎样才能做到这一点?谢谢

更新

所以在解析我的帖子类是enter image description here

所以我知道是谁上传了它但我不知道如何为这个特定用户检索个人资料图片。

2 个答案:

答案 0 :(得分:0)

好的,这可能对Objective-C

有所帮助
PFUser *user = PFUser *user = [PFUser currentUser]; 
[user fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
 _profileImage.file = [object objectForKey:@"profilePicture"]; 
}];

答案 1 :(得分:0)

您需要使用指向创建对象的用户的指针。个人资料照片应位于用户类中。然后,在查询中包含指针,该指针将返回用户数据。