无法使用模拟器从 iOS 中的 ImagePicker 获取图像路径和图像?
Posted
技术标签:
【中文标题】无法使用模拟器从 iOS 中的 ImagePicker 获取图像路径和图像?【英文标题】:Unabe to get image path & image from UIImagePicker in ios using simulator? 【发布时间】:2015-06-25 09:19:34 【问题描述】:我正在尝试从 imagepicker 控制器获取图像路径和选定的图像。我使用委托方法来获取路径和图像,但我总是得到 null。
图片选择器显示代码
- (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
【问题讨论】:
你在pick_image中得到了什么? 可以链接到模拟器,或者链接到这个:***.com/questions/3088874/… pickercontroller 中已经可以有图片了 ; 【参考方案1】:也许使用- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
会更好
喜欢
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
【讨论】:
【参考方案2】:使用这个委托方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
以下一个已弃用(您使用过)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo NS_DEPRECATED_ios(2_0, 3_0);
【讨论】:
【参考方案3】:尽管使用[editingInfo valueForKey:UIImagePickerControllerReferenceURL];
,请使用[editingInfo valueForKey:UIImagePickerControllerMediaURL]
,这将为您提供图像的路径。
【讨论】:
UIImagePickerControllerMediaURL 用于电影。以上是关于无法使用模拟器从 iOS 中的 ImagePicker 获取图像路径和图像?的主要内容,如果未能解决你的问题,请参考以下文章