1

I have an UIImageView which load profile picture of the user. I add some UIImagePicker features and now the user able to change the profile picture either by using camera or add from library. This is the code:

- (IBAction)cameraAction:(id)sender {
   UIImagePickerController *picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;
   picker.allowsEditing = YES;
   picker.sourceType = UIImagePickerControllerSourceTypeCamera;

   [self presentViewController:picker animated:YES completion:NULL];
}

- (IBAction)libraryAction:(id)sender {
   UIImagePickerController *picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;
   picker.allowsEditing = YES;
   picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

   [self presentViewController:picker animated:YES completion:NULL];
}

#pragma mark - Image Picker Controller delegate methods

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

   UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
   _profilePhoto.image = chosenImage;
   UIImageWriteToSavedPhotosAlbum(_profilePhoto.image, nil, nil, nil);

   [picker dismissViewControllerAnimated:YES completion:NULL];
}

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

   [picker dismissViewControllerAnimated:YES completion:NULL];

}

Now then, I had php files to upload the current image to server. The problem is, the input parameter for the php is image file name. I do looking around for quite some times and known that to get the image name, first I need to save it to apps document directory and rename it with the name I want later. So can anyone tell me how to save this image to apps document directory, how to get it and how to rename it with the one I want. Thanks in advance

1

1 Answer 1

3

Don't think that saving the file to document directory will generate you new name. When I save something to documents I generate unique file name.

What I do is I pass to web server some generic name like "Upload.png".

Here is some pseudo code:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
   UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
   [picker dismissViewControllerAnimated:YES completion:NULL];


   ...
   NSData *data = UIImagePNGRepresentation(choosenImage);
   NSString *name = @"Uploaded.png"

   // Upload the nsdata to server
   [[PHPServer instance] uploadProfileImage:data named:name];
}
Sign up to request clarification or add additional context in comments.

8 Comments

Then could you please tell me how to transform the choosenImage to NSData? Sorry for noob question :p @Krzysztof
Sorry did not get the question ?
Sorry, accidentally hit the enter button :P Please review my edited comment
No worries, please see updated answer. Not sure about your php method implementation, probably multipart encode. If so please have a look at this gist, looks good: gist.github.com/mombrea/8467128 You need to change Content-Disposition and Content-Type accordingly.
Okay, I think I understand your code. So what I need to pass to the php is the NSData and NSString instead of attached string to the image itself? Because the php I used only have string post parameter
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.