iOS UIImagePickerController權限聲明、判斷;拍照獲取相冊,照片、視頻,存儲本地相冊


1、相冊的權限 info.plist

<key>NSPhotoLibraryAddUsageDescription</key>

<string>需要打開存儲到相冊權限,保存照片/視頻到相冊</string>

<key>NSMicrophoneUsageDescription</key>

<string>錄制需要打開麥克風,視頻時使用</string>

<key>NSAppleMusicUsageDescription</key>

<string>需要獲取本地視頻權限,進行選擇視頻。。</string>

<key>NSPhotoLibraryUsageDescription</key>

<string>需要打開相冊權限,訪問相冊選擇照片</string>

<key>NSCameraUsageDescription</key>

<string>需要打開相機權限,進行拍照/錄像</string>

 

附( 在iOS里面調用系統相機、相冊顯示英文,需要修改成中文的方法

1、先把手機的語言模式設置成簡體中文

2、在Info.Plist里面把Localization native development region字段修改成China

3、在Info.Plist里面添加字段Localized resources can be mixed(Boolean)值為YES)

2、需要使用的系統庫、代理、初始化UIImagePickerController

#import <MobileCoreServices/MobileCoreServices.h>
#import <AssetsLibrary/AssetsLibrary.h>

//代理

<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

//初始化picker

。。。。

3、功能支持判斷

//判斷是否支持相機

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
}

4、觸發資源選擇 相機、 相冊

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"選取圖片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction * cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.pickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
[self presentViewController:self.pickerController animated:YES completion:nil];
}];

UIAlertAction * videoAction = [UIAlertAction actionWithTitle:@"拍視頻" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.pickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
///拍攝模式 此時不可以設置
// self.pickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
//視頻類型
self.pickerController.mediaTypes = @[(NSString*)kUTTypeMovie];
//視頻編碼質量
self.pickerController.videoQuality = UIImagePickerControllerQualityTypeMedium;
//視頻錄制時間。默認600s
self.pickerController.videoMaximumDuration = 15;

//模態視圖的彈出效果
self.pickerController.modalPresentationStyle=UIModalPresentationOverFullScreen;
[self presentViewController:self.pickerController animated:YES completion:nil];
}];

UIAlertAction * photosLibaryAction = [UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//設置媒體類型:照片public.image或視頻public.movie
// self.pickerController.mediaTypes = @[@"public.movie",@"public.image"];

//或者這樣設置
self.pickerController.mediaTypes = @[(NSString*)kUTTypeMovie,(NSString*)kUTTypeImage];
[self presentViewController:self.pickerController animated:YES completion:nil];
}];

UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];

//判斷是否支持相機
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[alert addAction:cameraAction];
[alert addAction:videoAction];
}

[alert addAction:photosLibaryAction];
[alert addAction:cancelAction];

[self presentViewController:alert animated:YES completion:nil];
}

 

 

5、判斷獲取資源類型\ 保存 本地

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{

//判斷資源是照片還是視頻
NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

//照片

UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:didFinishSavingWithError:contextInfo:),NULL);

[self.pickerController dismissViewControllerAnimated:YES completion:nil];

 

}else if([mediaType isEqualToString:(NSString *)kUTTypeMovie]){

//視頻

//視頻處理
[self.pickerController dismissViewControllerAnimated:YES completion:^{

//視頻的URL
NSURL *movieURL = info[UIImagePickerControllerMediaURL];

// //文件管理器
// NSFileManager* fm = [NSFileManager defaultManager];
//
// //創建視頻的存放路徑
// NSString * path = [NSString stringWithFormat:@"%@/tmp/video%.0f.merge.mp4", NSHomeDirectory(), [NSDate timeIntervalSinceReferenceDate] * 1000];
// NSURL *mergeFileURL = [NSURL fileURLWithPath:path];
//
// //通過文件管理器將視頻存放的創建的路徑中
// [fm copyItemAtURL:[info objectForKey:UIImagePickerControllerMediaURL] toURL:mergeFileURL error:nil];
// AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];
//
// //根據AVURLAsset得出視頻的時長
// CMTime time = [asset duration];
// int seconds = ceil(time.value/time.timescale);


//可以根據需求判斷是否需要將錄制的視頻保存到系統相冊中
// 判斷獲取類型:視頻

//獲取視頻文件的url
NSURL* mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
//創建ALAssetsLibrary對象並將視頻保存到媒體庫
// Assets Library 框架包是提供了在應用程序中操作圖片和視頻的相關功能。相當於一個橋梁,鏈接了應用程序和多媒體文件。
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
// 將視頻保存到相冊中
[assetsLibrary writeVideoAtPathToSavedPhotosAlbum:movieURL

completionBlock:^(NSURL *assetURL, NSError *error) {
NSString * alertString = @"";
if (!error) {
alertString = @"視頻已保存到相冊";
}else{
alertString = @"視頻保存到相冊失敗";

}

}];

}];

 

}

}

///保存照片 結果

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

{

NSString *msg = nil ;

if(error != NULL){

msg = @"保存圖片失敗" ;
}else{
msg = @"保存圖片成功" ;
}

NSLog(@"%@",msg);
[UIViewPopAlert pushAlertOneActionViewWithMessage:msg Target:self Title:@"提示" oneAlertTitle:@"確定" ChangeSystem:NO oneActionfunc:^{

}];

}

 

源代碼:Demo

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM