需要加载一个协议 UIImagePickerControllerDelegate
//从手机相册中获得图片并显示在imageView中
- (IBAction)albumClocked:(id)sender {
//创建一个图片选择器
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//设置选择器的资源类型
imagePicker.sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//设置代理
imagePicker.delegate = self;
//采用模态窗口 进行显示
[self presentViewController:imagePicker animated:YES completion:nil];
}
#pragma mark-imagePickerController方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"%@",info);
//让模态窗口miss
[picker dismissViewControllerAnimated:YES completion:nil];
//在info中通过key找到图片
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
//设置图片
[self.imageView setImage:self.image];
}
//创建一个图片选择器
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//设置选择器的资源类型
imagePicker.sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//设置代理
imagePicker.delegate = self;
//采用模态窗口 进行显示
[self presentViewController:imagePicker animated:YES completion:nil];
}
#pragma mark-imagePickerController方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"%@",info);
//让模态窗口miss
[picker dismissViewControllerAnimated:YES completion:nil];
//在info中通过key找到图片
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
//设置图片
[self.imageView setImage:self.image];
}