1 #import "ViewController.h" 2 3 @interface ViewController ()<UIImagePickerControllerDelegate> 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 13 btn.backgroundColor = [UIColor grayColor]; 14 btn.frame = CGRectMake(20, 30, 80, 80); 15 [btn addTarget:self action:@selector(showImagerPicker) forControlEvents:UIControlEventTouchUpInside]; 16 [self.view addSubview:btn]; 17 18 } 19 20 -(void)showImagerPicker{ 21 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];//创建对象 22 imagePicker.sourceType = 0;//设置类型(相册、相机) 23 24 imagePicker.allowsEditing = YES;//是否允许编辑,默认no,设置为yes时,点击图片会进入编辑界面(裁剪) 25 imagePicker.delegate = self; 26 [self presentViewController:imagePicker animated:YES completion:nil];//弹出相册/相机 27 } 28 29 //选择图片后调用的代理方法 30 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 31 32 NSLog(@"----%@",info); 33 34 UIImage *image = info[@"UIImagePickerControllerEditedImage"];//获取图片 35 UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 36 imageView.frame = CGRectMake(30, 140, 90, 90); 37 [self.view addSubview:imageView]; 38 39 [self dismissViewControllerAnimated:YES completion:nil];//消失imagepicker 40 41 } 42 43 - (void)didReceiveMemoryWarning { 44 [super didReceiveMemoryWarning]; 45 // Dispose of any resources that can be recreated. 46 } 47 48 @end
时间: 2024-11-05 15:50:11