我正在尝试获取图像路径&从imagepicker控制器中选择的图像。我使用了委托方法获取路径&图像,但我总是得到 null 。
显示imagepicker的代码
- (IBAction)choosePicture:(id)sender
{
UIImagePickerController *pickerController = [[UIImagePickerController alloc]
init];
pickerController.delegate = self;
[self presentViewController:pickerController animated:YES completion:nil];
}
代表代码
- (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:^{
}];
}
视图控制器的协议代码
#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
答案 0 :(得分:3)
使用- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
像
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
}
答案 1 :(得分:1)
使用此委托方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
不推荐使用一个(您使用过)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0);
答案 2 :(得分:1)
请使用[editingInfo valueForKey:UIImagePickerControllerMediaURL]
,[editingInfo valueForKey:UIImagePickerControllerReferenceURL];
这将为您提供图片的路径。