第一步:設(shè)置info,開(kāi)啟權(quán)限
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {?
?? [super viewDidLoad]; ? ?
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{? ??
//1.創(chuàng)建相冊(cè)選取器??
? UIImagePickerController *picker = [[UIImagePickerController alloc] init];??
? //2.設(shè)置代理 ?UIImagePickerController有兩個(gè)協(xié)議:UINavigationControllerDelegate? UIImagePickerControllerDelegate ? ?(注意:實(shí)現(xiàn)完代理方法一定要釋放picker)
picker.delegate = self; ??
?//3.設(shè)置來(lái)源 ? ?(注意:相機(jī)屬性一定要用真機(jī),模擬器不支持)
/**? ? UIImagePickerControllerSourceTypePhotoLibrary,相冊(cè)? (手機(jī)相簿)? ? UIImagePickerControllerSourceTypeCamera,相機(jī)(模擬器不支持)? ? UIImagePickerControllerSourceTypeSavedPhotosAlbum,相冊(cè)(手機(jī)照片)? ? */? ? picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;? ?
?//4.允許編輯? ?
?picker.allowsEditing = YES;?
?? //5.彈出相冊(cè)選取器??
? [self presentViewController:picker animated:YES completion:nil];
}
#pragma mark -UIImagePickerControllerDelegate/**完成選取的兩個(gè)方法為遞進(jìn)關(guān)系 *///2.完成選取? 該方法與下面方法1唯一的區(qū)別就是將編輯之后的圖片image作為參數(shù)暴露在外部,而方法二則需要從字典中獲取圖片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary*)editingInfo{
? ? [picker dismissViewControllerAnimated:YES completion:nil];? ? NSLog(@"%@",editingInfo);? ? NSLog(@"%@",image);}
//1.完成選取- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
[picker dismissViewControllerAnimated:YES completion:nil];
//? ? info中包括選取的照?片,視頻的主要信息
//? ? NSString *const UIImagePickerControllerMediaType; 選取的類(lèi)型 public.image public.movie
//? ? NSString *const UIImagePickerControllerOriginalImage; 修改前 的UIImage object.
//? ? NSString *const UIImagePickerControllerEditedImage; 修改后 的UIImage object.
//? ? NSString *const UIImagePickerControllerCropRect; 原始圖 ?片的尺?寸NSValue object containing a CGRect data type
//? ? NSString *const UIImagePickerControllerMediaURL; 視頻在?文件系統(tǒng)中 的 NSURL地址
//? ? 保存視 頻主要時(shí)通過(guò)獲取其N(xiāo)SURL 然后轉(zhuǎn)換成NSData
UIImage *image = info[UIImagePickerControllerEditedImage];
self.imageView.image = image;
NSLog(@"%@",info);
}
//取消選取
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
@end