I'm trying to upload an image as a PFFile for a user I have in Parse. I don't get any errors but the file isn't there? What am I doing wrong?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.imageToUpload = info[UIImagePickerControllerOriginalImage];
self.profileImageView.image = self.imageToUpload;
NSData *pictureData = UIImagePNGRepresentation(self.imageToUpload);
PFFile *image = [PFFile fileWithName:#"img" data:pictureData];
[image saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
PFUser *user = [PFUser currentUser];
user[#"picture"] = image;
[picker dismissViewControllerAnimated:YES completion:nil];
}
else {
[[[UIAlertView alloc] initWithTitle:#"Error" message:[error userInfo][#"error"] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}
} progressBlock:^(int percentDone) {
NSLog(#"Uploaded: %d%%", percentDone);
}];
}
You don't actually save the user, so the link between the user and the new file isn't made. This leaves the file floating around not connected to anything.
Basically, save the user after setting [#"picture"] = image.
Related
I have a problem.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[self dismissViewControllerAnimated:YES completion:^{
if (image == nil){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Try again !" message:#"Please try again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}else{
NSData *fileData;
NSString *fileName;
NSString *fileType;
fileData = UIImagePNGRepresentation(image);
fileName = #"profilePic.png";
fileType = #"Image";
PFFile *file = [PFFile fileWithName:fileName data:fileData];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"An error occurred!"
message:[NSString stringWithFormat:#"Please try sending your message again. %#",error.userInfo]
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}else{
[[PFUser currentUser]setObject:file forKey:#"profilePic"];
[[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeed, NSError* error){
if (!error) {
NSLog(#"New profile pic loaded.");
[self.activity stopAnimating];
[self.reloadPicBtn setHidden:NO];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Try again !" message:#"Please try again. Failed to load your new profile pic :(" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
}];
}
}];
}
}];
}
Well, reloadPicBtn is shown, but if I call a function which reloads a picture, It fails. And when I try to understand WTF is going on using NSLog it logs out a string. So code has been executed. But there is no result on screen. The function is the same which is called in viewDidLoad. But it works when it is called from viewDidLoad. And when it is called again there is no result.
That reload function:
- (void) loadProfilePic{
PFQuery* query = [PFUser query];
[query getObjectInBackgroundWithId:[PFUser currentUser].objectId block:^(PFObject* object, NSError* error){
if(!error){
PFFile* imageFile = object[#"profilePic"];
[imageFile getDataInBackgroundWithBlock:^(NSData* data, NSError* error){
if (!error) {
self.activity.hidden = NO;
[self.activity startAnimating];
self.profilePic.image = [UIImage imageWithData:data];
NSLog(#"Profile pic shown");
}
else{
NSLog(#"Error 2: %#",error);
}
}];
}else{
self.profilePic.image = [UIImage imageNamed:#"profile#2.png"];
NSLog(#"Fail 1 : %#",error);
}
}];
}
Please help me.
It prints out profile pic shown. But it doesn't even use network to download an image.
Ive seen many posting on here with phenomenal answers by the community. Unfortunately none have been able to help or guide me to what i need help with. Im trying to "fetch" the image that the "current user" uploaded to parse on the same "UIImageView"
below is the example of how i uploaded the photo
myaccount.h
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)UploadPhoto:(id)sender;
- (IBAction)selectPhoto:(id)sender;
myaccount.m
- (IBAction)UploadPhoto:(UIButton *)sender {
PFObject *User = [PFUser currentUser];
NSData *imageData = UIImageJPEGRepresentation(_imageView.image, 0.8);
NSString *filename = [NSString stringWithFormat:#"%#.png", _username.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[User setObject:imageFile forKey:#"ProfilePicture"];
// Show progress
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = #"Uploading";
[hud show:YES];
// Upload Profile Picture to Parse
[User saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
[hud hide:YES];
if (!error) {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Upload Complete" message:#"Successfully uploaded profile picture" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
// Dismiss the controller
//[self dismissViewControllerAnimated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Upload Failure" message:[error localizedDescription] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];
}
i honestly have no idea where to go from here! the photo uploads perfectly to the class on parse.com... but i cannot get it to show everytime the app viewdidloads this controller.
i tried
- (void)viewDidLoad {
[super viewDidLoad];
PFFile * myPFFile = [[PFUser currentUser] objectForKey:#"ProfilePicture"];
[myPFFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];
// image can now be set on a UIImageView
}
}];
but i get unused variable for "image" on UIImage *image = [UIImage imageWithData:data];
what am i doing wrong. Someone please help me with any codes I'm missing. I've been at this for 3 months now and its stopping me from moving forward.... PPPPLLZZZ help
What am I missing? Do you put the image inside an imageView?
(thats why you get 'unused variable for "image"')
in your case:
[imageView setImage:image];
I'm trying to create a New User using Parse.com as my backend. I was able to successfully register the user's info. But, my problem is saving the user's profile picture along with it. After the user selects the picture using the camera or photo library, I am presenting an image editor (https://github.com/heitorfr/ios-image-editor) using a .xib file. This all works fine with no problems. Then the image is placed in the UIImageView box and the user proceeds to fill in the Username, email and password and they click signup. The problem is after they sign up and all the info gets placed in the table via Parse, the image container seems to be empty. When I click on the image file, the picture is not there. I can't seem to figure out what I'm doing wrong.
View Did Load
- (void)viewDidLoad {
[super viewDidLoad];
self.addPhotoImageView.contentMode = UIViewContentModeScaleAspectFill;
// Create add photo popup
self.addPhotoActionSheet = [[UIActionSheet alloc] initWithTitle:#"Select source" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take photo", #"Choose photo", nil];
// Create image picker
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.editing = YES;
}
Image Picker Controller Delegate Methods
// Switches to the image editing view
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self.imagePicker dismissViewControllerAnimated:NO completion:nil];
if (self.imageEditorViewController == nil) {
self.imageEditorViewController = [[HFImageEditorViewController alloc] initWithNibName:#"ImageEditor" bundle:nil]; }
self.imageEditorViewController.sourceImage = image;
self.imageEditorViewController.rotateEnabled = NO;
self.imageEditorViewController.checkBounds = YES;
[self.imageEditorViewController reset:NO];
__weak typeof(self) weakSelf = self;
self.imageEditorViewController.doneCallback = ^(UIImage *editedImage, BOOL canceled) {
if (!canceled) {
[weakSelf.addPhotoImageView setImage:editedImage];
}
// Hide editor
[weakSelf.imageEditorViewController dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:self.imageEditorViewController animated:NO completion:nil]; }
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil]; }
UIActionSheetDelegate methods
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 1:
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
default:
return;
}
[self presentViewController:self.imagePicker animated:YES completion:nil]; }
Here is IBAction signup code:
- (IBAction)signup:(id)sender {
NSData *imageData = UIImageJPEGRepresentation(self.chosenImage, 0.5f);
PFFile *imageFile = [PFFile fileWithName:#"Profileimage.jpg" data:imageData];
NSString *username = [self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *email = [self.emailField.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([username length] == 0 || [email length] == 0 || [password length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Oops" message:#"Make sure you fill in all the fields!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else {
PFUser *newUser = [PFUser user];
newUser[#"ProfilePic"] = imageFile;
newUser.username = username;
newUser.email = email;
newUser.password = password;
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:[error.userInfo objectForKey:#"error"] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}];
}
}
There's no indication from the Parse.com API docs that any information other than password and username are set when using - signUpInBackgroundWithBlock:
You will have to sign up the user first, then have a nested call within the PFBooleanResultBlock of the signUp method to perform
else {
PFUser *newUser = [PFUser user];
newUser.username = username;
newUser.email = email;
newUser.password = password;
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:[error.userInfo objectForKey:#"error"] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else {
newUser[#"ProfilePic"] = imageFile;
[newUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (! error) {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}];
}
}];
}
You need to set it up to where you are sending a file to the database, since Parse will store images as such. Here's how I save images in my projects.
.h file:
#property (nonatomic, weak) IBOutlet UITextField *username
#property (nonatomic, weak) IBOutlet UITextField *email;
#property (nonatomic, strong) IBOutlet UIImageView *photo;
#property (nonatomic, strong) IBOutlet UIButton *addUserPhoto;
.m file in your save method
NSString *username = _username.text;
NSString *email = _email.text;
PFObject *newUser = [PFObject objectWithClassName:#"User"];
[newUser setObject:username forKey:#"username"];
[newUser setObject:email forKey:#"email"];
// image
NSData *imageData = UIImageJPEGRepresentation(_photo.image, 0.8);
NSString *filename = [NSString stringWithFormat:#"%#.png", _name.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[newUser setObject:imageFile forKey:#"profileImage"];
Replace _Photo with whatever you named the image view. It will set the uploaded image to this view.
If I save text without having any text in the field I get this error message in Parse.com: Update Failure - The operation couldn't be completed(Parse error 122.) If I press OK and then try to dismiss the view with Cancel(a button item) the app crashes. I think a valid file name at Parse.com has to contain at least 1 character. Maybe I can do do something to stop the user from saving when not enter text? Any ideas?
This my code:
- (IBAction)save:(id)sender {
// Create PFObject with profile information
PFUser *profile = [PFUser currentUser];
[profile setObject:nameTextField.text forKey:#"name"];
[profile setObject:titleTextField.text forKey:#"title"];
[profile setObject:locationTextField.text forKey:#"location"];
// Profile image
NSData *imageData = UIImageJPEGRepresentation(profileImageView.image, 0.8);
NSString *filename = [NSString stringWithFormat:#"%#", nameTextField.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[profile setObject:imageFile forKey:#"profileimageFile"];
// Show progress
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = #"Updating";
[hud show:YES];
// Upload profile to Parse
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0)
[hud hide:YES];
[profile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
[[[UIAlertView alloc] initWithTitle:#"Profile Information" message:#"Fill in atleast one field" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil]show];
[hud hide:YES];
}
else {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Successfully updated profile" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[hud hide:YES];
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"profile" sender:self];
}
}];
}
- (IBAction)Cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"profile" sender:self];
}
1
Test if your one letter theory is true. Change:
NSString *filename = [NSString stringWithFormat:#"%#", nameTextField.text];
To:
NSString *filename = [NSString stringWithFormat:#"file%#", nameTextField.text];
2
Or just avoid it if it's blank. So this:
NSString *filename = [NSString stringWithFormat:#"%#", nameTextField.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[profile setObject:imageFile forKey:#"profileimageFile"];
Becomes:
if (nameTextField.text) {
NSString *filename = [NSString stringWithFormat:#"%#", nameTextField.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[profile setObject:imageFile forKey:#"profileimageFile"];
}
3
Also, what is this:
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0)
It's doesn't appear to be connected to anything?
4
You call this twice in quick succession, and then again right after the file saves. Is there something in the method that makes the repetitive calls necessary?
[hud hide:YES];
5
Your if statement doesn't appear to be connected to anything:
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0)
I'm assuming you want:
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0) {
[hud hide:YES];
[profile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
[[[UIAlertView alloc] initWithTitle:#"Profile Information" message:#"Fill in atleast one field" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil]show];
[hud hide:YES];
}
else {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Successfully updated profile" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[hud hide:YES];
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"profile" sender:self];
}
}];
}
I have some issues with user photos (profile pictures). For every time a single user update his profile picture, it also updates the rest of the users with the same image. How do I change it so it doesn't change everywhere? I am using parse.com
I'm using following codes.
Uploading the photo:
- (void)uploadProfilePhoto {
NSData *imageData;
NSString *imageName;
NSString *imageType;
//Shrink it
//Upload profile photo
(self.image != nil); {
UIImage *resizeImage = [self resizeImage:self.image toWidth:320.0f andHeight:320.0f];
imageData = UIImagePNGRepresentation(resizeImage);
imageName = #"profileimage.png";
imageType = #"image";
}
PFFile *image = [PFFile fileWithName:imageName data:imageData];
[image saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Something went wrong!" message:#"Please try again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else {
PFObject *UserPhotos = [PFObject objectWithClassName:#"UserPhoto"];
[UserPhotos setObject:image forKey:#"image"];
[UserPhotos setObject:imageType forKey:#"imageType"];
[UserPhotos setObject:[PFUser currentUser] forKey:#"user"];
[UserPhotos setObject:[[PFUser currentUser] objectId] forKey:#"uploaderId"];
[UserPhotos setObject:[[PFUser currentUser] username] forKey:#"uploaderUsername"];
[UserPhotos saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Something went wrong!" message:#"Please try uploading your photo again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else {
//Everything was successfull!
}
}];
}
}];
}
When I retrieve the photo:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
PFQuery *query = [PFQuery queryWithClassName:#"UserPhoto"];
[query includeKey:#"user"];
[query orderByDescending:#"createdAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!object) {
return NSLog(#"%#", error);
}
PFFile *imageFile = object[#"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!data) {
return NSLog(#"%#", error);
}
// Do something with the image
self.profileImage.image = [UIImage imageWithData:data];
}];
}];
[self.navigationController.navigationBar setHidden:YES];
}
Thanks!