ios 發(fā)開之相機(jī)、相冊(cè)

#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;//相冊(cè)
@property(nonatomic, strong) AVCaptureSession *AVSession;
@end
//啟動(dòng)相冊(cè)
-(void)openPhoto{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"打開本地相冊(cè)" 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:@"對(duì)不起,未檢測到攝像頭,無法進(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) {
        //得到圖片
        //存入相冊(cè)
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        [self dismissViewControllerAnimated:YES completion:nil];
    } 
}


PS:在info.plist中添加下面的,可以打開相機(jī)相冊(cè)權(quán)限
相機(jī): <key>NSCameraUsageDescription</key>
<string>cameraDesciption</string>

相冊(cè): <key>NSPhotoLibraryUsageDescription</key>
<string>photoLibraryDesciption</string>

**實(shí)現(xiàn)調(diào)用相機(jī),相冊(cè)的demo中借鑒了諸多前輩的開發(fā)博客,在此根據(jù)自己開發(fā)中遇到的問題統(tǒng)一整理成此篇,向前輩們表示感謝和致敬。 ``

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容