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
armanb21
848 PointsHow would you retrieve an image from a UIImageView, and then upload it to Parse.com in Objective-C?
Hello Everyone,
I'm currently creating an app. One of the features in the app has the ability to send images. Currently you have to select a button that says 'Choose Image' and that button will appear with an imagePickerDialog where you can select a picture from your camera roll. After the user selects an image it will appear on the UIImageView. What I'm trying to do is take the image from the image view, and upload the image that is on the image view to my Parse.com database. Also, I've attached the code I've got on the method which saves the image to Parse. I'd appreciate any help!
Thanks in advance,
Arman
Note: This is an updated thread, with more information than the last one.
Methods where i save the message:
-(IBAction)saveCustomMessage{
NSString *title = _titleTextField.text;
NSString *note = _noteTextField.text;
NSString *explanation = _explainedTextField.text;
NSString *category = _categoryTextfield.text;
PFUser *toUser=nil;
if (public == NO) {
if ([self.usersTable indexPathForSelectedRow]){
toUser=[users objectAtIndex:[self.usersTable indexPathForSelectedRow].row];
}else{
NSLog(@"no user selected");
return;
}
} else{
uint32_t random = arc4random_uniform([users count]);
PFUser *randomUser = [users objectAtIndex:random];
toUser = randomUser;
}
PFObject *parseMessage = [PFObject objectWithClassName:@"message"];
parseMessage[@"picture"]=imageData;
UIImage *image = [_imageView image];
NSData *imageData = UIImagePNGRepresentation(image);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];
[parseMessage save];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message Sent"
message:@"Your message has been sent."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
Methods where I let the user choose the image:
- (IBAction)chooseImage:(id)sender {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
self.imageView.image=image;
[picker dismissViewControllerAnimated:NO completion:nil];
}
1 Answer
Dino Paškvan
Courses Plus Student 44,108 PointsEvery UIImageView has an image property you can get/set to change/retrieve the image inside the image view.
Then you get the PNG representation of that image and create a PFFile:
UIImage *image = [imageView image];
NSData *imageData = UIImagePNGRepresentation(image);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];
Then just assign that PFFile instance to an PFObject instance and save it.