iOS UIImagePickerController rights statement, judge; getting album pictures, photos, videos, photo albums stored locally

1 album authority info.plist

<key>NSPhotoLibraryAddUsageDescription</key>

<String> needs permission to open the store to the album, save the photo / video to the album </ string>

<key>NSMicrophoneUsageDescription</key>

<String> need to open microphone recording, use the video </ string>

<key>NSAppleMusicUsageDescription</key>

<String> need to obtain local video rights, to select the video. . </ String>

<key>NSPhotoLibraryUsageDescription</key>

<String> needs permission to open the album, select the photo album visit </ string>

<key>NSCameraUsageDescription</key>

<String> need permission to turn on the camera, take pictures / video </ string>

 

2, the system requires the use of libraries, agents, initialization UIImagePickerController

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

//proxy

<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

// Initialize picker

。。。。

3, function support judge

// determine whether to support the camera

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
}

4, trigger resource selection cameras, photo albums

-(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) {
= UIImagePickerControllerSourceTypeCamera self.pickerController.sourceType;
self.pickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
/// photographing mode at this time can not be set
// self.pickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
// video type
self.pickerController.mediaTypes = @ [(NSString *) kUTTypeMovie];
// video encoding quality
self.pickerController.videoQuality = UIImagePickerControllerQualityTypeMedium;
// video recording time. Default 600s
self.pickerController.videoMaximumDuration = 15;

pop-out effect // modal view of
self.pickerController.modalPresentationStyle = UIModalPresentationOverFullScreen;
[Self presentViewController: Animated self.pickerController: YES Completion: nil];
}];

UIAlertAction * photosLibaryAction = [UIAlertAction actionWithTitle: @ " Album" style: UIAlertActionStyleDefault Handler: ^ (* _Nonnull UIAlertAction Action) {
self.pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// set the media type: public.image photos or video 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, determine the type of access to resources \ saved locally

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

//判断资源是照片还是视频
NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType];

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

//photo

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

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

 

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

//video

// Video Processing
[self.pickerController dismissViewControllerAnimated: YES Completion: ^ {

// the URL of the video
NSURL movieURL * = info [UIImagePickerControllerMediaURL];

// // File Manager
// the NSFileManager FM * = [the NSFileManager defaultManager];
//
/ / // create a video storage path
// NSString * path = [NSString stringWithFormat: @ "% @ / tmp / video% .0f.merge.mp4", NSHomeDirectory (), [NSDate timeIntervalSinceReferenceDate] * 1000];
// NSURL mergeFileURL = * [NSURL fileURLWithPath: path];
//
// // path through the file Manager will create the video stored in
// [fm copyItemAtURL: [info objectForKey: UIImagePickerControllerMediaURL] toURL: mergeFileURL error: nil];
// * = Asset AVURLAsset [AVURLAsset assetWithURL: [NSURL fileURLWithPath: path]];
//
// // The length of the video obtained when AVURLAsset
// CMTime Time = [Asset DURATION];
// seconds The int = ceil (time.value / time.timescale);


video on demand // can determine whether to save the recorded the system album
// determines whether the acquired type: video

// get video file url
NSURL mediaUrl * = [info objectForKey: UIImagePickerControllerMediaURL];
// create ALAssetsLibrary object and save the video to the media library
// Assets library is to provide a framework package It features pictures and videos related to the operation of the application. The equivalent of a bridge to link the applications and multimedia files.
* = AssetsLibrary ALAssetsLibrary [[ALAssetsLibrary the alloc] the init];
// save videos into albums
[assetsLibrary writeVideoAtPathToSavedPhotosAlbum: movieURL

completionBlock: ^ (* assetUrl NSURL, the NSError * error) {
NSString * @ alertString = "";
IF (error! ) {
alertString = @ "saved to video album";
} the else {
alertString = @ "to save the video album failed";

}

}];

}];

 

}

}

/// save photos results

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

{

NSString * msg = nil;

IF (error = NULL!) {

Msg = @ "to save the image failed";
} the else {
msg = @ "Save Picture success";
}

NSLog (@ "% @", msg);
[UIViewPopAlert pushAlertOneActionViewWithMessage: msg Target: self Title: @ " Tip" oneAlertTitle: @ "OK" ChangeSystem: oneActionfunc NO: ^ {

}];

}

 

Source: Demo

 

Guess you like

Origin www.cnblogs.com/xujiahui/p/12160878.html