Unabe to get image path & image from UIImagePicker in ios using simulator?

1.4k Views Asked by At

I am trying to get the image path & selected image from imagepicker controller.I have used delegate method for getting the path & image but i am always getting null.

Code for showing imagepicker

- (IBAction)choosePicture:(id)sender
{
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]
                                                 init];
    pickerController.delegate = self;
    [self presentViewController:pickerController animated:YES completion:nil];


}

Code for delegate

- (void) imagePickerController:(UIImagePickerController *)picker
         didFinishPickingImage:(UIImage *)image
                   editingInfo:(NSDictionary *)editingInfo
{

    //You can retrieve the actual UIImage
    UIImage *pick_image = [editingInfo valueForKey:UIImagePickerControllerOriginalImage];

    NSURL *path = [editingInfo valueForKey:UIImagePickerControllerReferenceURL];
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
}

protocol code for view controller

#import <UIKit/UIKit.h>

@interface RegisterViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *txt_fname;
@property (weak, nonatomic) IBOutlet UITextField *txt_lname;
@property (weak, nonatomic) IBOutlet UITextField *txt_profession;
@property (weak, nonatomic) IBOutlet UITextField *txt_language;
@property (weak, nonatomic) IBOutlet UITextField *txt_email;
@property (weak, nonatomic) IBOutlet UITextField *txt_password;
@property (weak, nonatomic) IBOutlet UITextField *txt_confirm_password;
@property (weak, nonatomic) IBOutlet UITextField *txt_profile;
@property (weak, nonatomic) IBOutlet UITextField *txt_address;

@end
3

There are 3 best solutions below

0
On BEST ANSWER

Maybe it better to use - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;

like

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
}
1
On

Please use [editingInfo valueForKey:UIImagePickerControllerMediaURL] inspite of [editingInfo valueForKey:UIImagePickerControllerReferenceURL]; this will give you the path of the image.

0
On

Use this delegate method

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

Below one is deprecated (you used)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0);