美文网首页
ios 发开之相机、相册

ios 发开之相机、相册

作者: 墨凌风起 | 来源:发表于2017-03-03 18:26 被阅读18次
    #import <MediaPlayer/MediaPlayer.h>
    #import <MobileCoreServices/MobileCoreServices.h>
    #import <AVFoundation/AVFoundation.h>
    #import <AssetsLibrary/AssetsLibrary.h>
    @interface XXViewController ()
    <
    UIImagePickerControllerDelegate,
    UINavigationControllerDelegate  //相机导航栏
    >
    @property(nonatomic, strong) UIImagePickerController *CamreaPicker;//相机
    @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:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self transferCameraAndPhoto:200];
        }]];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    -(void)transferCameraAndPhoto:(NSInteger)SkinType{ 
        switch (SkinType) {
            case 100:
                //判断是否可以打开相机
                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:@"对不起,未检测到摄像头,无法进行拍照" preferredStyle:UIAlertControllerStyleAlert];
                    [alert addAction:[UIAlertAction actionWithTitle:@"确认" 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];
        }
    }
    
    //关闪光灯
    
    -(void)closeFlashLight{
        [self.AVSession stopRunning];
    }
    
    //相机回调函数
    -(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中添加下面的,可以打开相机相册权限
    相机: <key>NSCameraUsageDescription</key>
    <string>cameraDesciption</string>

    相册: <key>NSPhotoLibraryUsageDescription</key>
    <string>photoLibraryDesciption</string>

    **实现调用相机,相册的demo中借鉴了诸多前辈的开发博客,在此根据自己开发中遇到的问题统一整理成此篇,向前辈们表示感谢和致敬。 ``

    相关文章

      网友评论

          本文标题:ios 发开之相机、相册

          本文链接:https://www.haomeiwen.com/subject/ysqrgttx.html