一.從相冊(cè)里面選擇圖片到App中
1.選擇單張
1>.UIImagePickerController(自帶選擇界面的)
2>.AssetsLibrary(選擇界面需要開發(fā)者自己搭建)
3>.Photos(選擇界面需要開發(fā)者自己搭建)
2.多張圖片選擇(圖片數(shù)量>2)
1>.AssetsLibrary
2>.Photos
二.利用相機(jī)拍一張照片到APP
1>.UIImagePickerController
2>.AVcaptureSeccion.可以定義拍照界面樣式
選擇單張照片
/// 選擇照片
- (IBAction)selectPhoto:(UIButton *)sender {
UIAlertController *alterConroller = [UIAlertController alertControllerWithTitle:@"請(qǐng)選擇方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"照相機(jī)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self openCamera];
}];
UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"相冊(cè)" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self openAlbum];
}];
[alterConroller addAction:albumAction];
[alterConroller addAction:cameraAction];
[self presentViewController:alterConroller animated:YES completion:nil];
}
/// 打開照相機(jī)
- (void)openCamera{
//UIImagePickerControllerSourceTypePhotoLibrary, 從所有相冊(cè)選擇
//UIImagePickerControllerSourceTypeCamera, //拍一張照片
//UIImagePickerControllerSourceTypeSavedPhotosAlbum//從moments選擇一張照片
//判斷照相機(jī)能否使用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
/// 打開相冊(cè)
- (void)openAlbum{
//UIImagePickerControllerSourceTypePhotoLibrary, 從所有相冊(cè)選擇
//UIImagePickerControllerSourceTypeCamera, //拍一張照片
//UIImagePickerControllerSourceTypeSavedPhotosAlbum//從moments選擇一張照片
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark - ******UIImagePickerControllerDelegate******
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
[picker dismissViewControllerAnimated:YES completion:nil];
self.imageView.image = info[UIImagePickerControllerOriginalImage];
NSLog(@"%@",info);
}
- 注意事項(xiàng)
UIImagePickerController的代理有2個(gè)遵循2個(gè)代理<UINavigationControllerDelegate, UIImagePickerControllerDelegate>