1.準備atom
//須要遵照兩個協議spa
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>代理
//xib中拉一個按鈕(跳轉) 一個接收image的UIImageView(連線以下)圖片
@property (weak, nonatomic) IBOutlet UIImageView *imgV;it
@property (nonatomic, strong) UIImagePickerController *picker;io
@end方法
2.設置im
_picker = [[UIImagePickerController alloc]init];協議
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {img
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
//設置代理
_picker.delegate = self;
3.進入相冊 present (xib連線的)
- (IBAction)sender:(UIButton *)sender {
[self presentViewController:_picker animated:YES completion:^{}];
}
4.設置UIImagePickerController的代理方法實現所要的效果 (1.取消時 2選擇了圖片時)
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"cancel");
[picker dismissViewControllerAnimated:YES completion:^{}];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
// 取原始照片
_imgV.image = info[@"UIImagePickerControllerOriginalImage"];
// 返回
[picker dismissViewControllerAnimated:YES completion:^{}];
// 將info打印 看看是什麼
// NSLog(@"%@",info);//結果以下
2016-06-18 20:28:56.488 UIPickerController[5531:306107] {
UIImagePickerControllerMediaType = "public.image";
UIImagePickerControllerOriginalImage = "<UIImage: 0x7fcefe02e650> size {3000, 2002} orientation 0 scale 1.000000";
UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=ED7AC36B-A150-4C38-BB8C-B6D696F4F2ED&ext=JPG";
}
}
運行結果以下: