#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface XXViewController ()
<
UIImagePickerControllerDelegate,
UINavigationControllerDelegate //相機(jī)導(dǎo)航欄
>
@property(nonatomic, strong) UIImagePickerController *CamreaPicker;//相機(jī)
@property(nonatomic, strong) UIImagePickerController *PhotoPicker;//相冊
@property(nonatomic, strong) AVCaptureSession *AVSession;
@end
//啟動相冊
-(void)openPhoto{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"打開本地相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self transferCameraAndPhoto:100];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"打開相機(jī)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self transferCameraAndPhoto:200];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
-(void)transferCameraAndPhoto:(NSInteger)SkinType{
switch (SkinType) {
case 100:
//判斷是否可以打開相機(jī)
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
_CamreaPicker = [[UIImagePickerController alloc]init];
_CamreaPicker.delegate = self;
_CamreaPicker.allowsEditing = YES;
//攝像頭
_CamreaPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:_CamreaPicker animated:YES completion:nil];
}else{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"對不起跋理,未檢測到攝像頭择克,無法進(jìn)行拍照" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"確認(rèn)" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alert animated:YES completion:nil];
}
break;
case 200:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
_PhotoPicker = [[UIImagePickerController alloc]init];
_PhotoPicker.delegate = self;
_PhotoPicker.allowsEditing = YES;
_PhotoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_PhotoPicker animated:YES completion:nil];
}
break;
default:
break;
}
}
//開閃光燈
-(void)openFlashLight{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device.torchMode == AVCaptureTorchModeOff) {
self.AVSession = [[AVCaptureSession alloc]init];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
[_AVSession addInput:input];
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc]init];
[_AVSession addOutput:output];
[_AVSession beginConfiguration];
[device lockForConfiguration:nil];
// Set torch to on
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
[_AVSession commitConfiguration];
// Start the session
[_AVSession startRunning];
// Keep the session around
[self setAVSession:self.AVSession];
}
}
//關(guān)閃光燈
-(void)closeFlashLight{
[self.AVSession stopRunning];
}
//相機(jī)回調(diào)函數(shù)
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//獲取圖片名字
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
//獲取圖片的名字
if (image !=nil) {
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){
ALAssetRepresentation *representation = [myasset defaultRepresentation];
NSString *photoName = [representation filename];
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:imageURL resultBlock:resultblock failureBlock:nil];
[picker dismissViewControllerAnimated:true completion:nil];
}
if (picker == _CamreaPicker) {
//得到圖片
//存入相冊
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[self dismissViewControllerAnimated:YES completion:nil];
}
}
PS:在info.plist中添加下面的,可以打開相機(jī)相冊權(quán)限
相機(jī): <key>NSCameraUsageDescription</key>
<string>cameraDesciption</string>
相冊: <key>NSPhotoLibraryUsageDescription</key>
<string>photoLibraryDesciption</string>
**實現(xiàn)調(diào)用相機(jī)前普,相冊的demo中借鑒了諸多前輩的開發(fā)博客肚邢,在此根據(jù)自己開發(fā)中遇到的問題統(tǒng)一整理成此篇,向前輩們表示感謝和致敬。 ``