一.从相册里面选择图片到App中
1.选择单张
1>.UIImagePickerController(自带选择界面的)
2>.AssetsLibrary(选择界面需要开发者自己搭建)
3>.Photos(选择界面需要开发者自己搭建)
2.多张图片选择(图片数量>2)
1>.AssetsLibrary
2>.Photos
二.利用相机拍一张照片到APP
1>.UIImagePickerController
2>.AVcaptureSeccion.可以定义拍照界面样式
选择单张照片
/// 选择照片
- (IBAction)selectPhoto:(UIButton *)sender {
UIAlertController *alterConroller = [UIAlertController alertControllerWithTitle:@"请选择方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self openCamera];
}];
UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self openAlbum];
}];
[alterConroller addAction:albumAction];
[alterConroller addAction:cameraAction];
[self presentViewController:alterConroller animated:YES completion:nil];
}
/// 打开照相机
- (void)openCamera{
//UIImagePickerControllerSourceTypePhotoLibrary, 从所有相册选择
//UIImagePickerControllerSourceTypeCamera, //拍一张照片
//UIImagePickerControllerSourceTypeSavedPhotosAlbum//从moments选择一张照片
//判断照相机能否使用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
/// 打开相册
- (void)openAlbum{
//UIImagePickerControllerSourceTypePhotoLibrary, 从所有相册选择
//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);
}
- 注意事项
UIImagePickerController的代理有2个遵循2个代理<UINavigationControllerDelegate, UIImagePickerControllerDelegate>