Reputation: 107
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
Upvotes: 1
Views: 1123
Reputation: 1471
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];
}
Upvotes: 3