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
Mike Lee
6,579 PointsHelp 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
Stone Preston
42,016 Pointsif 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];
Mike Lee
6,579 PointsSo how what exactly what would I put in my imageView.image = ?
Stone Preston
42,016 Pointsif you are using PFImageView the loadInBackground method takes care of that
Mike Lee
6,579 PointsI 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'
Stone Preston
42,016 Pointsdo you have your image views class set to PFImageView?
Mike Lee
6,579 PointsYes in my .h file I have @property(strong,nonatomic) PFImageView *imageView;
Stone Preston
42,016 Pointsare you using storyboards?
Mike Lee
6,579 PointsYes and I connected the image View with the click and drag to the .h file
Stone Preston
42,016 Pointsclick 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
Mike Lee
6,579 Pointsok I changed the class and removed and reconnected the connection,but I still get the same error
Stone Preston
42,016 Pointsok 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];
Mike Lee
6,579 PointsThe 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];
}
Mike Lee
6,579 Pointsany ideas?
Stone Preston
42,016 Pointsdo you still get the same error when it crashes?
Mike Lee
6,579 PointsYes it gives the same error
Stone Preston
42,016 Pointsdid you try logging the class name like I said
Mike Lee
6,579 Pointsyes but I figured it out, in the app delegate did finishFinishLaunching I had to put [PFImageView class];