UIImagePickerController
資料來源型別
UIImagePickerControllerSourceTypePhotoLibrary 相 冊
UIImagePickerControllerSourceTypeCamera 拍照 攝像
UIImagePickerControllerSourceTypeSavedPhotosAlbum 時刻
媒體型別
mediaTypes:
public.image//是圖片型別
public.movie//是視訊型別
相簿,照相
//相簿
- (IBAction)selectImage:(id)sender {
//相簿的資源訪問UIImagePickerController來讀取
UIImagePickerController *imagePickerC = [[UIImagePickerController alloc]init];
//資料來源型別
imagePickerC.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;//相簿
//是否允許編輯
imagePickerC.allowsEditing = YES;
imagePickerC.delegate = self;
[self presentViewController:imagePickerC animated:YES completion:NULL];
}
//照相
- (IBAction)takePhoto:(id)sender {
/*
UIImagePickerControllerCameraDeviceRear:後置攝像頭
UIImagePickerControllerCameraDeviceFront:前置攝像頭
*/
//判斷是否有攝像頭
BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
if (!isCamera) {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"沒有可用的攝像頭" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertC addAction:action];
[self presentViewController:alertC animated:YES completion:NULL];
return;
}
UIImagePickerController *imagePickerC = [[UIImagePickerController alloc]init];
//資料來源的型別
imagePickerC.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerC.delegate = self;
imagePickerC.allowsEditing = YES;
[self presentViewController:imagePickerC animated:YES completion:NULL];
}
視訊,拍攝
//視訊
- (IBAction)video:(id)sender {
UIImagePickerController *imagePickerC = [[UIImagePickerController alloc]init];
imagePickerC.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerC.delegate = self;
//指定媒體型別 (預設public.image )
imagePickerC.mediaTypes = @[@"public.movie"];//public.image
[self presentViewController:imagePickerC animated:YES completion:NULL];
}
//拍攝
- (IBAction)shoot:(id)sender {
//判斷是否有攝像頭
BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
if (!isCamera) {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"沒有可用的攝像頭" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertC addAction:action];
[self presentViewController:alertC animated:YES completion:NULL];
return;
}
UIImagePickerController *imagePickerC = [[UIImagePickerController alloc]init];
//資料流型別:拍攝
imagePickerC.sourceType =UIImagePickerControllerSourceTypeCamera;
imagePickerC.delegate = self;
//指定媒體型別 (預設public.image )
imagePickerC.mediaTypes = @[@"public.movie"];//public.image
[self presentViewController:imagePickerC animated:YES completion:NULL];
}
UIImagePickerController代理方法
#pragma mark - UIImagePickerController Delegate
//完成選擇之後
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(@"info = %@",info);
//獲取媒體型別
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]) { //照片
/*
UIImagePickerControllerEditedImage:編輯後的圖片
UIImagePickerControllerOriginalImage:編輯前的圖片
*/
UIImage *img = info[UIImagePickerControllerEditedImage];
self.myImageView.image = img;
//如果是照相
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//儲存到相簿中
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}else if ([mediaType isEqualToString:@"public.movie"]){ //視訊
NSURL *url = info[UIImagePickerControllerMediaURL];
//播放視訊 MPMoviePlayerViewController需要引入框架
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
//彈出視訊播放器,自動播放
[picker presentViewController:moviePlayer animated:YES completion:NULL];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"取消選擇");
[self dismissViewControllerAnimated:YES completion:NULL];
}
//照片儲存成功的回撥方法 注意:方法名命名有要求
- (void) image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo{
NSLog(@"儲存成功");
}
注意:
照相,拍攝是需要攝像頭的,因為模擬器沒有攝像頭,所以需要用真機模擬。