1

I've been trying to figure this out via research and plowing through the docs for a few days and I can't get it / I don't understand it.

Here's what I'm trying to achieve:

enter image description here

I have a custom class named "ProfilePhotos" and a standard User class. I have some code that is "working" when I dump images onto the main User class in a column called "profilePicture". I need to get the image from 'imageFile' in the ProfilePhotos class.

I CAN figure out how to create images into one class (ProfilePhotos), but CAN'T figure out how to call them up while gathering data from the User to whom the image belongs ("Name" field).

The code I have that works, but has some weird bugs (images load in the wrong order, not at all, crashes) is below:

@interface CollectionViewController () <UITextViewDelegate>

@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong)  PFObject *theName;

@end

@implementation CollectionViewController

- (instancetype) init { //INIT STUFF }
-(void) viewDidLoad { //VIEWDIDLOAD STUFF }

-(void) DataCalls {

    PFQuery *getPhotos = [PFUser query];
    [getPhotos findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            NSLog(@"Objects Are:%@", objects);

            _imageArray = [[NSMutableArray alloc] initWithArray:objects];

            for (PFObject* photo in objects  ) {
                PFFile *pictureFile = [photo objectForKey:@"profilePicture"];
                _theName = [photo objectForKey:@"Name"];
                [self.collectionView reloadData];
                NSLog(@"Name is:%@", _theName);
                [pictureFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                    if (!error) {

                        NSLog(@"Fetching image..");
                        [_imageArray addObject:[UIImage imageWithData:data]];
                        NSLog(@"Size of the _imageArray : %lu", (unsigned long)[_imageArray count]);

                    } else {
                        // Log details of the failure
                        NSLog(@"Error: %@ ", error);
                    }
                }];

            }
        }
    }];    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photo" forIndexPath:indexPath];

    _imageView = (UIImageView *)[cell viewWithTag:200];


    PFObject *imageObject = [_imageArray objectAtIndex:indexPath.row];
    PFFile *imageFile = [imageObject objectForKey:@"profilePicture"];

    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            _imageView.image = [UIImage imageWithData:data];

        }
    }];

    PFObject *nameText = [imageObject objectForKey:@"Name"];
    NSLog(@"Name in the CV:%@", nameText);
    cell.userName.text= imageObject[@"Name"];

    return cell;


}
4
  • You must need a separated class for the profile images? You would like to store the previous profile images? Commented Jun 12, 2014 at 16:40
  • I do need two classes because I need to know createdAt date of the image and createdAt data of the user (separately). Also, isn't it a better practice to not put everything on one giant class? Commented Jun 12, 2014 at 17:06
  • It's not a good practice to make a monster from your User class, but i think in this case it's much easier to store and retrieve profile images from there. Usually you display the recent profile image of a user, therefore the createdAt doesn't required. However if you wanna store the previous profile images of a user, use a different class, but if you need only the latest image what she uploaded as a profile image go with the User class. Commented Jun 12, 2014 at 17:18
  • createdAt for the profile image IS required though. I am able to make and retrieve the profile images from the main User class no problem. If you know how to do profile images in their own class while getting relational user data, I'd love to see how to do it. That's my hang up :) Commented Jun 12, 2014 at 17:43

2 Answers 2

1

Keep using pointers, don't switch to using username to link things. Just remember to use includeKey: on your query to return fully populated pointers, e.g.:

PFQuery *query = [PFQuery queryWithClassName:@"ProfilePhotos"]

// apply any filters, here's how to filter by user:
// could just as easily be another user
PFUser *user = [PFUser currentUser];
[query whereKey:@"user" equalTo:user];

// this allows reading of user properties in the results
[query includeKey:@"user"];

[query orderByDescending:@"createdAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (error) {
        NSLog(@"Error.");
    } else {
        PFFile *file = object[@"imageFile"];
        PFUser *pictureUser = object[@"user"];
        NSString *name = pictureUser[@"Name"];
    }
}];
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. Not only is this it, it's got the learning baked in for me. I know how to do this now thanks to you. Hat tip Timothy. Big thanks.
0

Try out this, it should work.

PFQuery *query = [PFQuery queryWithClassName:@"ProfilePhotos"];
[query whereKey:@"user" equalTo:desiredUsername];
[query orderByDescending:@"createdAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (error) {
        NSLog(@"Error.");
    } else {
        PFFile *file = [object objectForKey:@"imageFile"];
        //Now you can pass it to the image view to display the image
    }
}];

3 Comments

so if I wanted to get a column labeled "Name" (not username) from the user class is it : [query whereKey:@"user" equalTo:"Name"]; ?
If you want one exact user from the Name column, for example the current user do user.Name or user.name, because you need a string, and Name is string property of the User class (but declare a current user object called user). [query whereKey:@"user" equalTo:user.Name];
Going to try this now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.