一.從相簿裡面選擇圖片到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>