//选择照片

- (IBAction)selectedPhoto:(id)sender {

    

    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];

    

    /*

     UIImagePickerControllerSourceTypePhotoLibrary, 显示所有文件夹

     UIImagePickerControllerSourceTypeCamera,   调用系统摄像头

     UIImagePickerControllerSourceTypeSavedPhotosAlbum 显示内置文件夹

     */

    pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    

    pickVC.delegate = self;

    

    [self presentViewController:pickVC animated:YES completion:nil];

    

}


//拍照

- (IBAction)cameraPhoto:(id)sender {

    //判断当前设备是否有摄像头

    BOOL isCamer = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];

    if (!isCamer) {

        

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有可用摄像头" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

        [alertView show];

        return;

        

    }

    

    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];

    

    //指定资源的来源:来自摄像头

    pickVC.sourceType = UIImagePickerControllerSourceTypeCamera;

    

    pickVC.delegate = self;

    

    [self presentViewController:pickVC animated:YES completion:NULL];

    

}


//选择视频资源

- (IBAction)selectVedio:(id)sender {

    

    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];

    

    /*

     UIImagePickerControllerSourceTypePhotoLibrary, 显示所有文件夹

     UIImagePickerControllerSourceTypeCamera,   调用系统摄像头

     UIImagePickerControllerSourceTypeSavedPhotosAlbum 显示内置文件夹

     */

    pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    

    pickVC.delegate = self;

    

    //指定选择媒体资源类型 public.movie 视频类型  public.p_w_picpath 照片类型

    pickVC.mediaTypes = @[@"public.movie"];

    

    [self presentViewController:pickVC animated:YES completion:nil];


}


//拍视频

- (IBAction)camerVadio:(id)sender {

    

    //判断当前设备是否有摄像头

    BOOL isCamer = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];

    if (!isCamer) {

        

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有可用摄像头" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

        [alertView show];

        return;

        

    }


    

    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];

    

    /*

     UIImagePickerControllerSourceTypePhotoLibrary, 显示所有文件夹

     UIImagePickerControllerSourceTypeCamera,   调用系统摄像头

     UIImagePickerControllerSourceTypeSavedPhotosAlbum 显示内置文件夹

     */

    pickVC.sourceType = UIImagePickerControllerSourceTypeCamera;

    

    pickVC.delegate = self;

    

    //指定选择媒体资源类型 public.movie 视频类型  public.p_w_picpath 照片类型

    pickVC.mediaTypes = @[@"public.movie"];

    

    [self presentViewController:pickVC animated:YES completion:nil];


    

}


#pragma mark -UIImagePickerControllerDelegate

//选择一个照片、或者视频调用的协议方法

- (void)p_w_picpathPickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    

    if ([mediaType isEqualToString:@"public.p_w_picpath"]) {

        //图片

        //获取图片

        UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];

        self.imgView.p_w_picpath = img;

        

        //判断照片的来源是否来自摄像头

        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

            //将图片保存至相册

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

        }

        

    }else if([mediaType isEqualToString:@"public.movie"]) {

        

        //        UIImagePickerControllerReferenceURL

        

        //1.获取到此资源的url地址

        NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];

        

        //2.创建视频播放器

        MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

        

        //3.弹出视频播放控制器,自动播放

        [self presentMoviePlayerViewControllerAnimated:moviePlayer];

    }


    

    //关闭模态视图

    [picker dismissViewControllerAnimated:YES completion:nil];

}


//通过UIImageWriteToSavedPhotosAlbum 保存图片成功以后,调用的方法

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

{

    NSLog(@"保存成功");

}