Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Help with parse.com and UIImage

Hi i have a image stored on parse.com and I m trying to get that image and put it into my UIImageView.What am I doing wrong

Here is my code:

-(void)viewWillAppear:(BOOL)animated {

PFUser *currentuser = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:@"User"];
[query whereKey:@"username" equalTo:currentuser];
[query whereKey:@"profilePic" equalTo:@"image.png"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (!error) {
        PFFile *file = [object objectForKey:@"image"];
        // file has not been downloaded yet, we just have a handle on this file

        // Tell the PFImageView about your file
        imageView.file = file;

        // Now tell PFImageView to download the file asynchronously
        [imageView loadInBackground];
    }
}];

}

10 Answers

if your image is an attribute of your user class, I dont think you need to query for it. Just access it from your currentUser.

PFUser *currentuser = [PFUser currentUser];
PFFile *file = [currentUser objectForKey:@"profilePic"];
// Tell the PFImageView about your file
imageView.file = file;
// Now tell PFImageView to download the file asynchronously
[imageView loadInBackground];

So how what exactly what would I put in my imageView.image = ?

if you are using PFImageView the loadInBackground method takes care of that

I get this error from the imageView.file line

-[UIImageView setFile:]: unrecognized selector sent to instance 0x93e5350 2014-05-06 12:46:59.086 NextUp[65564:15503] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView setFile:]: unrecognized selector sent to instance 0x93e5350'

do you have your image views class set to PFImageView?

Yes in my .h file I have @property(strong,nonatomic) PFImageView *imageView;

are you using storyboards?

Yes and I connected the image View with the click and drag to the .h file

click on your imageView in your storyboard and open the identity inspector. Make sure you have PFImageView set as its custom class. If you dont have that as its class type it in. remove your old property declaration and remove the connection using the connections inspector. Then click and drag again to your .h file to connect it again

ok I changed the class and removed and reconnected the connection,but I still get the same error

ok try logging the class name before calling the method to verify the class is correct

PFUser *currentuser = [PFUser currentUser];
NSString *className = NSStringFromClass([imageView class]);
NSLog(@"imageView class:%@", className);
PFFile *file = [currentUser objectForKey:@"profilePic"];
// Tell the PFImageView about your file
imageView.file = file;
// Now tell PFImageView to download the file asynchronously
[imageView loadInBackground];

The app runs then crashes when I click on my profile tab

I dont know if some of my other code is interfering but heres some of it:

  • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; self.imageView.image = chosenImage;

    NSData *imageData = UIImagePNGRepresentation(chosenImage); PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData]; [imageFile saveInBackground];

    PFUser *user = [PFUser currentUser]; [user setObject:imageFile forKey:@"profilePic"]; [user saveInBackground];

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

  • (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

any ideas?

do you still get the same error when it crashes?

Yes it gives the same error

did you try logging the class name like I said

yes but I figured it out, in the app delegate did finishFinishLaunching I had to put [PFImageView class];